PHP - Abstract Classes and Methods
Some methods only make sense once a child class fills in the details. An abstract method is exactly that: the parent declares it but gives it no implementation, leaving that work to the child class or classes.
A class with at least one abstract method is called an abstract class. Its purpose is enforcement: every derived class (child class) has to implement the abstract methods declared by the parent.
Both abstract classes and abstract methods are marked with the abstract keyword.
Here is how that looks in practice:
Example
Car is abstract and declares an abstract intro() method, which its two child classes each implement:
Output: PHP script
<?php
// Abstract base class
abstract class Car {
public $name;
// Non-abstract method
public function __construct($name) {
$this->name = $name;
}
// Abstract method - forces child classes to implement it
abstract public function intro();
}
// Child class that extends the abstract class
class Audi extends Car {
public function intro() {
return "German quality! I'm an $this->name!";
}
}
// Child class that extends the abstract class
class Citroen extends Car {
public function intro() {
return "French extravagance! I'm a $this->name!";
}
}
// Create objects of the child classes
$audi = new audi("Audi");
echo $audi->intro();
echo "<br>";
$citroen = new citroen("Citroen");
echo $citroen->intro();
?>
Example Explained
Audi and Citroen both extend the abstract Car class. Because of that inheritance, they can use its public $name property and its public __construct() method.
intro() is different. It is abstract, so each child class has to provide its own implementation.
Notes on Abstract Classes
A child class has to follow some rules when it implements an abstract method. The method must keep the same name, and its access modifier must be the same or less restrictive. For example, an abstract method declared protected can stay protected or become public in the child, but it can never become private. The type and number of required arguments must also match, although the child is free to add optional arguments.
To sum up, these rules apply when a child class inherits from an abstract class:
- The method in the child class has the same name as the abstract method and redeclares it
- Its access modifier is the same as the parent's or less restrictive
- It takes the same number of required arguments, and it may add optional ones
PHP - Abstract Method with Argument
Abstract methods can take arguments too. The next two examples show how that works:
Example
The abstract method declares one argument, $name, and the child class implements it with that same argument:
Output: PHP script
<?php
abstract class ParentClass {
// Abstract method with an argument
abstract protected function prefixName($name);
}
class ChildClass extends ParentClass {
public function prefixName($name) {
if ($name == "John Doe") {
$prefix = "Mr.";
} elseif ($name == "Jane Doe") {
$prefix = "Mrs.";
} else {
$prefix = "";
}
return "$prefix $name";
}
}
$class = new ChildClass;
echo $class->prefixName("John Doe");
echo "<br>";
echo $class->prefixName("Jane Doe");
echo "<br>";
echo $class->prefixName("Baby Doe");
?>
Example
Here, the child class adds two optional arguments that the parent's abstract method does not declare:
Output: PHP script
<?php
abstract class ParentClass {
// Abstract method with an argument
abstract protected function prefixName($name);
}
class ChildClass extends ParentClass {
// The child class may define optional arguments that are not in the parent's abstract method
public function prefixName($name, $separator = ".", $greet = "Dear") {
if ($name == "John Doe") {
$prefix = "Mr";
} elseif ($name == "Jane Doe") {
$prefix = "Mrs";
} else {
$prefix = "";
}
return "$greet $prefix$separator $name";
}
}
$class = new ChildClass;
echo $class->prefixName("John Doe");
echo "<br>";
echo $class->prefixName("Jane Doe");
?>