📡 You're offline — showing cached content
New version available!
Quick Access
PHP Beginner

PHP Inheritance: Reuse Code with Extends

Learn PHP inheritance — child classes extend parents, override methods, and call parent:: .

EzyCoders Admin May 20, 2026 3 min read 9 views
PHP Inheritance: Reuse Code with Extends
Share: Twitter LinkedIn WhatsApp

What is it?

Inheritance lets a child class reuse all the code of a parent class and add or override behaviour. The parent holds shared logic; children specialise it.

Why does it matter?

Without inheritance you would duplicate shared logic across every class. A Dog and Cat class both eat and sleep — put that in an Animal parent class and inherit it rather than duplicating it.

Learn PHP inheritance — child classes extend parents, override methods, and call parent:: .

Real-World Use Cases

  • 🐾 Animal taxonomy - Animal parent → Dog, Cat, Bird children each inherit eat() and sleep() but override makeSound().
  • 👤 User roles - User parent → AdminUser, GuestUser children that inherit getProfile() but override getPermissions().
  • 💳 Payment methods - Payment parent → CreditCard, UPI, NetBanking children that share processRefund() but override pay().
  • 📄 Document types - Document parent → Invoice, Receipt, PurchaseOrder children sharing a header/footer but different body templates.

Parent and Child Class

<?php

/*
--------------------------------
Parent and Child Class in PHP
(Inheritance)
--------------------------------
*/

// Parent Class
class Animal {

    public $name;

    public function eat() {

        echo $this->name . " is eating.<br>";
    }
}

// Child Class
class Dog extends Animal {

    public function bark() {

        echo $this->name . " is barking.<br>";
    }
}

// Create Object
$dog = new Dog();

// Access Parent Property
$dog->name = "Tommy";

// Access Parent Method
$dog->eat();

// Access Child Method
$dog->bark();

?>

Overriding Methods

<?php

/*
--------------------------------
Method Overriding in PHP
--------------------------------
*/

// Parent Class
class Animal {

    public function sound() {

        echo "Animal makes a sound.<br>";
    }
}

// Child Class
class Dog extends Animal {

    // Override Parent Method
    public function sound() {

        echo "Dog barks.<br>";
    }
}

// Another Child Class
class Cat extends Animal {

    // Override Parent Method
    public function sound() {

        echo "Cat meows.<br>";
    }
}

// Objects
$animal = new Animal();
$dog = new Dog();
$cat = new Cat();

/*
--------------------------------
Call Methods
--------------------------------
*/

$animal->sound();

$dog->sound();

$cat->sound();

?>

Calling the Parent with parent::

<?php

/*
--------------------------------
Calling Parent Method using parent::
--------------------------------
*/

// Parent Class
class Animal {

    public function sound() {

        echo "Animal makes a sound.<br>";
    }
}

// Child Class
class Dog extends Animal {

    // Override Method
    public function sound() {

        // Call Parent Method
        parent::sound();

        // Child Class Code
        echo "Dog barks.<br>";
    }
}

// Object
$dog = new Dog();

/*
--------------------------------
Call Method
--------------------------------
*/

$dog->sound();

?>

Q: Can a child class have multiple parent classes in PHP?

No. PHP supports single inheritance only. Use interfaces for contracts and traits for code reuse across unrelated classes.

EzyCoders Admin
Written by
EzyCoders Admin

Team Lead and Full-Stack Developer with experience in PHP, JavaScript, SQL, DSA, and System Design. Passionate about software engineering, scalable web technologies, and helping developers prepare for coding interviews and tech careers through practical tutorials and professional guidance.

Comments (0)

No comments yet. Be the first!

Leave a Comment