PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP Inheritance

Inheritance is a key feature of object-oriented programming. It allows you to define a class based on another class. This makes creating and maintaining an application much easier. The class that is inherited from is called the parent or superclass, and the class that inherits from the parent is called the child or subclass.

Here's a basic example of inheritance in PHP:

class Vehicle {
    public $wheels;

    public function setWheels($wheels) {
        $this->wheels = $wheels;
    }
}

class Car extends Vehicle {
    public $doors;

    public function setDoors($doors) {
        $this->doors = $doors;
    }
}

In this example, Car is a subclass of Vehicle. This means that Car inherits all public and protected methods and properties of Vehicle. You can use these just as if they were directly inside Car.

You can use the Car class like this:

$myCar = new Car();
$myCar->setWheels(4);
$myCar->setDoors(4);
echo $myCar->wheels; // Outputs: 4
echo $myCar->doors; // Outputs: 4

In the code above, even though the setWheels() method is defined in the Vehicle class, you can use it in an instance of the Car class. This is because Car inherits from Vehicle.

One important thing to note about inheritance is the visibility of properties and methods:

  • Public properties/methods are accessible everywhere.
  • Protected properties/methods are accessible only within the class itself and by inherited and parent classes.
  • Private properties/methods are accessible only within the class that defines the method or property.

If you have a method in the child class with the same name as a method in the parent class, the child class method will override the parent class method. This is known as method overriding. If you still want to call the parent class method, you can do so with parent::methodName().

  1. Creating and extending classes in PHP:

    • Create a class and extend it to another class.
    <?php
    // Base class
    class Animal {
        public function makeSound() {
            echo 'Generic animal sound';
        }
    }
    
    // Subclass extending Animal
    class Dog extends Animal {
        public function makeSound() {
            echo 'Bark';
        }
    }
    
    // Create instances
    $animal = new Animal();
    $dog = new Dog();
    
    // Output sounds
    $animal->makeSound(); // Output: Generic animal sound
    $dog->makeSound();    // Output: Bark
    
  2. Inheriting properties and methods in PHP:

    • Inherited properties and methods from the base class.
    <?php
    // Base class
    class Shape {
        protected $color;
    
        public function setColor($color) {
            $this->color = $color;
        }
    }
    
    // Subclass extending Shape
    class Square extends Shape {
        public function draw() {
            echo 'Drawing a square with color ' . $this->color;
        }
    }
    
    // Create instances
    $square = new Square();
    $square->setColor('red');
    
    // Output drawing
    $square->draw(); // Output: Drawing a square with color red
    
  3. Access modifiers (public, private, protected) in PHP inheritance:

    • Use access modifiers for controlling visibility.
    <?php
    // Base class
    class Car {
        protected $model;
    
        public function setModel($model) {
            $this->model = $model;
        }
    }
    
    // Subclass extending Car
    class SportsCar extends Car {
        public function displayModel() {
            echo 'Model: ' . $this->model;
        }
    }
    
    // Create instances
    $sportsCar = new SportsCar();
    $sportsCar->setModel('Ferrari');
    
    // Output model
    $sportsCar->displayModel(); // Output: Model: Ferrari
    
  4. Overriding methods in PHP subclasses:

    • Override methods in subclasses.
    <?php
    // Base class
    class Animal {
        public function makeSound() {
            echo 'Generic animal sound';
        }
    }
    
    // Subclass extending Animal
    class Dog extends Animal {
        public function makeSound() {
            echo 'Bark';
        }
    }
    
    // Create instances
    $animal = new Animal();
    $dog = new Dog();
    
    // Output sounds
    $animal->makeSound(); // Output: Generic animal sound
    $dog->makeSound();    // Output: Bark
    
  5. Abstract classes and methods in PHP inheritance:

    • Use abstract classes and methods for defining blueprints.
    <?php
    // Abstract class
    abstract class Shape {
        protected $color;
    
        abstract public function draw();
    
        public function setColor($color) {
            $this->color = $color;
        }
    }
    
    // Concrete subclass extending Shape
    class Square extends Shape {
        public function draw() {
            echo 'Drawing a square with color ' . $this->color;
        }
    }
    
    // Create instance
    $square = new Square();
    $square->setColor('blue');
    
    // Output drawing
    $square->draw(); // Output: Drawing a square with color blue
    
  6. Final classes and preventing further inheritance in PHP:

    • Use final to prevent further subclassing.
    <?php
    // Final class
    final class Vehicle {
        // Class definition
    }
    
    // Attempt to extend a final class (will result in an error)
    // class Car extends Vehicle {
    //     // Class definition
    // }
    
  7. Using parent and self keywords in PHP inheritance:

    • Utilize parent and self keywords for referencing parent and current class.
    <?php
    // Base class
    class Animal {
        protected $name;
    
        public function setName($name) {
            $this->name = $name;
        }
    }
    
    // Subclass extending Animal
    class Dog extends Animal {
        public function bark() {
            echo 'Woof!';
        }
    
        public function setName($name) {
            // Use parent::setName to access the parent class method
            parent::setName($name);
            // Or use self::setName to access the current class method
            // self::setName($name);
        }
    }
    
    // Create instance
    $dog = new Dog();
    $dog->setName('Buddy');
    
    // Output name
    echo $dog->name; // Output: Buddy