ADVERTISEMENT
ADVERTISEMENT

What is Inheritance?

Inheritance in PHP is a feature of object-oriented programming that allows one class (child class) to use the properties and methods of another class (parent class). It helps in reusing code, saving time, and reducing duplication. The child class can also have its own additional features or modify inherited ones. Inheritance creates a relationship between classes, making code more organized and easier to maintain. Understand the concept inheritance with suitable exampels.

Why Use Inheritance in PHP?

  • Code reusability: Avoids repetition by using existing functionality.

  • Maintainability: Easier to update code since changes in the parent class automatically reflect in child classes.

  • Extensibility: Allows adding new features without modifying existing code.

Basic Syntax of Inheritance in PHP

PHP uses the extends keyword to create a child class from a parent class.

class ParentClass
{
    // Properties and methods
}

class ChildClass extends ParentClass
{
    // Additional properties and methods
}

Example of Basic Inheritance:

class ParentClass {
    public $name = "John";
    
    public function greet() {
        return "Hello, " . $this->name;
    }
}

class ChildClass extends ParentClass {
    public function sayGoodbye() {
        return "Goodbye, " . $this->name;
    }
}

$child = new ChildClass();
echo $child->greet();   // Output: Hello, John
echo $child->sayGoodbye();  // Output: Goodbye, John

Here, ChildClass inherits ParentClass, so it has access to name and greet().It has its own method sayGoodbye().

Accessing Parent Methods

Use parent:: to call a parent method inside the child class.

<?php
class Vehicle {
    public function start() 
   {
        echo "Vehicle starts.\n";
    }
}

class Car extends Vehicle 
    {
     public function start()
     {
        parent::start(); // Call parent method
        echo "Car engine starts.\n";
      }
}

$car = new Car();
$car->start(); 
// Output:
// Vehicle starts.
// Car engine starts.
?>

Inheriting Properties

Inheriting properties means a child class automatically gains the properties (variables) of its parent class. This promotes code reusability and reduces duplication. The child class can use, override, or extend these inherited properties as needed.

<?php
class Person {
    public $name = "John";

    public function introduce() {
        echo "Hi, I am $this->name.\n";
    }
}

class Student extends Person {
    public $school = "ABC School";

    public function info() {
        echo "$this->name studies at $this->school.\n";
    }
}

$student = new Student();
$student->introduce(); // Hi, I am John.
$student->info();      // John studies at ABC School.
?>

Explanation:

  • Student inherits $name and introduce() from Person.
  • It also has its own $school property and info() method.

Access Modifiers with Inheritance

Access modifiers control the visibility of properties and methods during inheritance.

  • public: Accessible everywhere, including child classes.
  • protected: Accessible within the class and its child classes.
  • private: Accessible only within the class, not inherited by child classes.
<?php
class Base {
    public $pub = "Public";
    protected $pro = "Protected";
    private $pri = "Private";

    public function show() {
        echo "$this->pub, $this->pro, $this->pri\n";
    }
}

class Derived extends Base {
    public function showDerived() {
        echo "$this->pub, $this->pro\n"; // Can’t access $this->pri
    }
}

$obj = new Derived();
$obj->show();         // Public, Protected, Private
$obj->showDerived();  // Public, Protected
// $obj->pri; // Error: Cannot access private property
?>

 What is the final keyword in PHP?

The final keyword in PHP prevents inheritance or method overriding. When used with a class, it cannot be extended. When used with a method, child classes cannot override that method, ensuring the original behavior remains unchanged.

<?php
final class BaseClass {
    // Can't be inherited
}

class ChildClass extends BaseClass { // Error!
    // Not allowed
}

class Demo {
    final public function show() {
        echo "Can't override this method.\n";
    }
}

class Test extends Demo {
    // public function show() {} // Error!
}
?>

 


ADVERTISEMENT

ADVERTISEMENT