0
0
PhpHow-ToBeginner · 4 min read

How to Use Inheritance in PHP: Syntax and Examples

In PHP, inheritance allows a class to extend another class using the extends keyword, so it can reuse and override properties and methods. The child class inherits all public and protected members from the parent class, enabling code reuse and easier maintenance.
📐

Syntax

Inheritance in PHP is done by creating a child class that extends a parent class. The child class inherits all public and protected properties and methods from the parent. You can override methods in the child class to change behavior.

  • class ChildClass extends ParentClass: Declares inheritance.
  • parent::methodName(): Calls the parent class method inside the child.
php
<?php
class ParentClass {
    public function greet() {
        echo "Hello from Parent\n";
    }
}

class ChildClass extends ParentClass {
    public function greet() {
        echo "Hello from Child\n";
    }
}
?>
💻

Example

This example shows a parent class Animal with a method makeSound(). The child class Dog inherits from Animal and overrides makeSound() to provide a specific sound. It also calls the parent method using parent::makeSound().

php
<?php
class Animal {
    public function makeSound() {
        echo "Some generic sound\n";
    }
}

class Dog extends Animal {
    public function makeSound() {
        parent::makeSound();
        echo "Bark!\n";
    }
}

$dog = new Dog();
$dog->makeSound();
?>
Output
Some generic sound Bark!
⚠️

Common Pitfalls

Common mistakes when using inheritance in PHP include:

  • Trying to access private properties or methods of the parent class (they are not inherited).
  • Forgetting to call parent::__construct() in the child constructor if the parent has one.
  • Overriding methods without calling the parent method when needed.

Example of forgetting to call parent constructor:

php
<?php
class ParentClass {
    public function __construct() {
        echo "Parent constructor called\n";
    }
}

class ChildClass extends ParentClass {
    public function __construct() {
        // Missing parent::__construct();
        echo "Child constructor called\n";
    }
}

$child = new ChildClass();

// Correct way:
class ChildClassFixed extends ParentClass {
    public function __construct() {
        parent::__construct();
        echo "Child constructor called\n";
    }
}

$childFixed = new ChildClassFixed();
?>
Output
Child constructor called Parent constructor called Child constructor called
📊

Quick Reference

  • extends: Keyword to inherit from a parent class.
  • parent::method(): Call a method from the parent class.
  • public/protected: Members accessible in child classes.
  • private: Members NOT inherited.
  • parent::__construct(): Call parent constructor inside child constructor.

Key Takeaways

Use extends to create a child class that inherits from a parent class.
Child classes inherit public and protected properties and methods, but not private ones.
Override parent methods in the child class to change behavior, optionally calling parent::method().
Always call parent::__construct() in child constructors if the parent has one.
Inheritance helps reuse code and organize related classes logically.