PHP Inheritance
Understanding PHP Inheritance (OOP Fundamentals)
PHP inheritance is a core object-oriented programming (OOP) concept that allows one class to inherit properties and methods from another class. This code reusability mechanism is fundamental in PHP development and web application architecture.
Key Inheritance Terminology in PHP
- Parent Class (Base Class/Superclass): The class being inherited from
- Child Class (Derived Class/Subclass): The class that inherits from another
- extends keyword: PHP syntax used to establish inheritance
- Method Overriding: Redefining parent class methods in child class
- Access Modifiers: public, protected, private visibility controls
Implementing Inheritance in PHP
Basic Inheritance Syntax
<?php
class ParentClass {
// properties and methods
}
class ChildClass extends ParentClass {
// additional properties and methods
}
?>
Types of Inheritance in PHP
- Single Inheritance: A child class inherits from one parent class
- Multilevel Inheritance: A chain of inheritance (grandparent → parent → child)
- Hierarchical Inheritance: Multiple classes inherit from a single parent
Note: PHP doesn't support multiple inheritance (one class extending multiple classes) directly, but similar functionality can be achieved with traits.
Access Modifiers and Inheritance
Understanding visibility scopes is crucial for proper encapsulation:
<?php
class Vehicle {
public $make; // accessible anywhere
protected $model; // accessible in child classes
private $vin; // accessible only in Vehicle
}
?>
Method Overriding in PHP Inheritance
Child classes can override parent methods to provide specific implementations:
<?php
class Animal {
public function makeSound() {
return "Generic animal sound";
}
}
class Dog extends Animal {
public function makeSound() {
return "Bark!";
}
}
?>
The parent Keyword
Access parent class methods using parent::
<?php
class ChildClass extends ParentClass {
public function myMethod() {
parent::myMethod(); // Calls parent version first
// Additional child-specific code
}
}
?>
PHP Inheritance Best Practices
- Favor composition over inheritance when appropriate
- Use abstract classes for partial implementations
- Implement interfaces for polymorphic behavior
- Keep inheritance hierarchies shallow to avoid complexity
- Document inheritance relationships clearly in code comments
Advanced Inheritance Concepts
Abstract Classes and Methods
<?php
abstract class DatabaseDriver {
abstract public function connect();
public function disconnect() {
// Common implementation
}
}
?>
Final Classes and Methods
Prevent further inheritance or overriding:
<?php
final class FinalClass {
// Cannot be extended
}
class Parent {
final public function finalMethod() {
// Cannot be overridden
}
}
?>
Inheritance vs. Other OOP Concepts
- Inheritance vs. Interfaces: Inheritance shares implementation, interfaces define contracts
- Inheritance vs. Traits: Traits provide horizontal code reuse (multiple behaviors)
Performance Considerations
- Deep inheritance hierarchies may impact performance
- Method calls in deep chains have slight overhead
- Modern PHP versions (8.0+) optimize inheritance well
Real-World PHP Inheritance Examples
E-commerce System:
<?php
class Product {
protected $price;
public function getPrice() { /* ... */ }
}
class Book extends Product {
private $author;
public function getDetails() { /* ... */ }
}
class Electronics extends Product {
private $warranty;
public function getWarrantyInfo() { /* ... */ }
}
?>
CMS Application:
<?php
class Content {
protected $title;
public function display() { /* ... */ }
}
class Article extends Content {
private $author;
public function display() { /* custom implementation */ }
}
class Video extends Content {
private $duration;
public function display() { /* video-specific display */ }
}
?>