Skip to main content

PHP OOP Inheritance (Extending Classes)

By SamK
0
0 recommends
Category(s)
Topic(s)

In PHP OOP, one class can be based on another class. This behavior is called inheritance.

The child class inherits all public and protected properties and methods from the parent class, while also having the ability to define its own properties and methods.

To create an inherited class, use the extends keyword.

Example:

<?php
// Base class
class Animal {
   public function makeSound() {
       echo "This animal makes a sound.\n";
   }
}

// Derived class
class Dog extends Animal {
   public function makeSound() {
       echo "The dog barks: Woof! Woof!\n";
   }
}

// Using the parent class
$animal = new Animal();
$animal->makeSound(); // Output: This animal makes a sound.

// Using the child class
$dog = new Dog();
$dog->makeSound(); // Output: The dog barks: Woof! Woof!
?>

In the example above, the Dog class is inherited from the Animal class.

This means that the Dog class can use the makeSound() method from the Animal class because of inheritance.

The Dog class, however, overrides the makeSound() method to provide its own behavior, demonstrating how inheritance allows for customization in the child class.

Inheritance and the Protected Access Modifier

When a property or method is marked as protected in PHP, it means:

  • Classes that inherit (extend) the parent class can access the protected properties or methods as if they were part of the child class.
  • It provides a level of encapsulation, where protected members are hidden from external access but still available to child classes for further use or customization.
  • This strikes a balance between private (only within the class) and public (accessible to everyone) visibility.

Example

<?php
// Base class
class Animal {
   protected $name;
   public function __construct($name) {
       $this->name = $name; // Accessible within this class and child classes
   }
   protected function describe() {
       echo "This is an animal named $this->name.\n";
   }
}

// Derived class
class Dog extends Animal {
   public function makeSound() {
       echo "$this->name barks: Woof! Woof!\n";
   }
   public function describeDog() {
       // Accessing the protected method of the parent class
       $this->describe();
   }
}

// Creating an instance of the child class
$dog = new Dog("Buddy");

// Accessing the child class method
$dog->makeSound(); // Output: Buddy barks: Woof! Woof!

// Accessing a method that uses the protected parent method
$dog->describeDog(); // Output: This is an animal named Buddy.
?>

In the example above, everything works fine because the protected method (describe()) is accessed from within the derived class (Dog) using the describeDog() method.

Overriding Inherited Methods

In PHP, method overriding occurs when a child class defines a method with the same name as a method in its parent class. The child class's version of the method replaces (or overrides) the parent class's version when called from an instance of the child class.

This allows the child class to provide a specific implementation for the method while still retaining inheritance.

<?php
// Base class
class Animal {
   public function makeSound() {
       echo "This animal makes a sound.\n";
   }
}

// Derived class
class Dog extends Animal {
   // Overriding the makeSound method
   public function makeSound() {
       echo "The dog barks: Woof! Woof!\n";
   }
}

// Another derived class
class Cat extends Animal {
   // Overriding the makeSound method
   public function makeSound() {
       echo "The cat meows: Meow! Meow!\n";
   }
}

// Creating instances and calling the overridden methods
$animal = new Animal();
$animal->makeSound(); // Output: This animal makes a sound.

$dog = new Dog();
$dog->makeSound(); // Output: The dog barks: Woof! Woof!

$cat = new Cat();
$cat->makeSound(); // Output: The cat meows: Meow! Meow!
?> 

The final Keyword

The final keyword can be used to restrict class inheritance or to prevent a method from being overridden.

Example

<?php
// Define a final class
final class Car {
   public function drive() {
       echo "Driving the car!";
   }
}
// Trying to extend a final class will result in an error
// This will cause a fatal error
class SportsCar extends Car {
   // ...
}

/*
Output:
PHP Fatal error: Class SportsCar may not inherit from final class (Car) in /path/to/file/script.php on line 16
*/
?> 

Preventing Method Overriding

<?php
class Animal {
   // Define a final method
   final public function makeSound() {
       echo "Some animal sound";
   }
}
class Dog extends Animal {
   // Trying to override a final method will cause an error
   // This will cause a fatal error
   public function makeSound() {
       echo "Bark!";
   }
}

/*
Output:
PHP Fatal error: Cannot override final method Animal::makeSound() in /path/to/file/script.php on line 18
*/
?> 

In the above example, the makeSound method in the Animal class is marked as final. This means the method cannot be overridden in the Dog class, and trying to do so will result in a fatal error, as you can see in this output.

Questions & Answers