In this comprehensive article, we will go deep into the realm of PHP abstract classes. This article will offer you with a full overview of how to utilize abstract classes in your PHP development projects, from comprehending the principles to examining the syntax and implementation. This article will provide you with the knowledge and abilities to successfully work with abstract classes in your code, whether you are a newbie or an experienced developer.
An abstract class contains the declaration of methods but not their definition. It only contains their names. It is the responsibility of the child class to define these methods.
What is an abstract method?
An abstract method is a method of an abstract class that is declared using abstract keywords and does not contain the body. It is defined by the child’s class.
- abstract keyword is used to declare an abstract class or method.
- An abstract class must contains at least one abstract method. However, it can also contains non-abstract methods as well.
Syntax
<?php
abstract class ParentClass {
abstract public function someMethod1();
abstract public function someMethod2($name, $color);
abstract public function someMethod3() : string;
}
?>
What are the rules of abstract class in PHP?
- An abstract class must contains at least one abstract method. However, it can contains non-abstract methods as well.
- When a child called inherit the abstract parent class, it must defines the abstract methods of parent class with the same name.
- While defining the asbtract methods in the child class, it should be defined with less restricted access modifier. For instance, if the parent class contains the abstract method having protected access modifier. So, when the child class defines this method, it should keep its access modifier as either protected or public. It cannot set it to private, becasue it is more restricted than the protected.
- The child class defining the abstract method must pass the equal number of arguments as specified in the declration in the parent asbtract class. However, the child class can have optional/extra arguments other than those required.
Example of Abstract class in PHP
Explanation of the above example
<!-- wp:code -->
<pre class="wp-block-code"><code><?php
// Parent abstract class
abstract class Bike {
public $name;
public function __construct($name) {
$this->name = $name;
}
abstract public function introduction() : string;
}
// Child classes defining the parent classes
class Honda extends Bike {
public function intro() : string {
return "I am $this->name!";
}
}
class Suzuki extends Bike {
public function introduction() : string {
return "I am $this->name!";
}
}
// Objects from child classes
$honda = new honda("Honda");
echo $honda->introduction();
$suzuki= new suzuki("Suzuki");
echo $suzuki->introduction();
?></code></pre>
<!-- /wp:code -->
- In the above example, we create an abstract class Bike that contains an abstract method introduction.
- We create two child classes Honda and Suzuki that extends the abstract class and defines the introduction method.
- We create the objects of these classes and call the introduction method using their objects. The introduction method works according to the implementation given by its corresponding class.
Example of abstract class with extra arguments in method overriding
<?php
// Parent abstract class
abstract class Bike {
public $name;
public function __construct($name) {
$this->name = $name;
}
abstract protected function introduction($model) : string;
}
// Child classes defining the parent classes
class Honda extends Bike {
public function intro($model) : string {
return "I am $this->name. My model is $model";
}
}
class Suzuki extends Bike {
public function introduction($model, $color=null) : string {
return "I am $this->name. My model is $model and color is $color";
}
}
// Objects from child classes
$honda = new honda("Honda");
echo $honda->introduction();
$suzuki= new suzuki("Suzuki");
echo $suzuki->introduction();
?>
Explanation of the above example
- The basic logic of the above example is like the previous example. However, we pass extra argument to the function in the introduction method of suzuki class.
- As we have define the rule earlier in this tutorial, the access modifier of the child class while implementing the abstract method of the parent class must be less than as specified in the parent class for the declaration of this method. So, both the child classes uses public access modifier to define the introduction method.
- Try to define the function with private access modifier, it will produce an error.
Note: The abstract class does not contain any constructor method. So, we cannot create the instance of an abstract class.