Category(s)
Topic(s)
Abstract classes and methods are used when the parent class defines a method that must be implemented by its child class(es).
An abstract class contains at least one abstract method. An abstract method is declared but not implemented in the parent class. The child class is required to provide the method's implementation.
To define an abstract class or method, we use the abstract
keyword:
<?php
abstract class BaseClass {
// Abstract method without implementation, must be defined in child classes
abstract public function methodOne();
// Abstract method with parameters, must be defined in child classes
abstract public function methodTwo($name, $color);
// Abstract method with a return type, must be defined in child classes
abstract public function methodThree(): string;
}
?>
When a child class inherits from an abstract class, the following rules must be followed:
- The child class method must be defined with the same name and must override the abstract method from the parent class.
- The child class method must use the same or a less restrictive access modifier than the parent’s abstract method. For example, if the abstract method is defined as protected, the method in the child class can be protected or public, but it cannot be private.
- The number of required arguments must match, though the child class can include additional optional parameters.
Example 1
<?php
// Parent class
abstract class Animal {
public $name;
// Constructor to initialize the name
public function __construct($name) {
$this->name = $name;
}
// Abstract method to be implemented by child classes
abstract public function intro(): string;
}
// Child classes
class Dog extends Animal {
// Implementing the abstract method from the parent class
public function intro(): string {
return "I'm a loyal companion! I'm a $this->name!";
}
}
class Cat extends Animal {
// Implementing the abstract method from the parent class
public function intro(): string {
return "I'm independent and graceful! I'm a $this->name!";
}
}
// Create objects from the child classes and display their messages
$dog = new Dog("Dog");
echo $dog->intro();
echo "<br>";
$cat = new Cat("Cat");
echo $cat->intro();
/*
Output:
I'm a loyal companion! I'm a Dog!
I'm independent and graceful! I'm a Cat!
*/
?>
Example 2: Abstract method includes arguments
<?php
abstract class Person {
// Abstract method with two arguments
abstract protected function getFullTitle($name, $gender);
}
class Titles extends Person {
public function getFullTitle($name, $gender) {
// Adding prefixes based on the gender
if ($gender === "Male") {
$title = "Mr.";
} elseif ($gender === "Female") {
$title = "Ms.";
} else {
$title = "Mx.";
}
// Return the full title
return "{$title} {$name}";
}
}
// Create an instance of the Titles class
$titles = new Titles;
// Display the full title for each name
echo $titles->getFullTitle("Alice Johnson", "Female");
echo "<br>";
echo $titles->getFullTitle("Bob Martin", "Male");
/*
Output:
Ms. Alice Johnson
Mr. Bob Martin
*/
?>
Example 3: Child class introduces an optional argument
<?php
abstract class Person {
// Abstract method with two arguments
abstract protected function getFullTitle($name, $gender);
}
class Greeting extends Person {
// The child class introduces an optional argument
public function getFullTitle($name, $gender, $salutation = "Hello") {
if ($gender === "Male") {
$title = "Mr.";
} elseif ($gender === "Female") {
$title = "Ms.";
} else {
$title = "Mx.";
}
// Return the full title
return "{$salutation} {$title} {$name}";
}
}
$person = new Greeting;
echo $person->getFullTitle("Alice Smith", "Female");
echo "<br>";
echo $person->getFullTitle("Bob Smith", "Male");
/*
Output:
Hello Ms. Alice Smith
Hello Mr. Bob Smith
*/
?>