PHP has abstract classes and methods. Classes defined as abstract cannot be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method’s signature; they cannot define the implementation.
When inheriting from an abstract class, all methods marked abstract in the parent’s class declaration must be defined by the child class, and follow the usual inheritance and signature compatibility rules.
- Abstract class
Not only can you define a template for children, but Abstract Classes offer the added benefit of letting you define the functionality that your child classes can utilize later.
File – code1.php
<?php abstract class A{ abstract function car(); } class TATA extends A{ function car(){ echo “car brand is: TATA”; } } $nexas = new TATA(); $nexas->car(); ?> |

Let’s see another example of abstract class…
File – code2.php
<?php abstract class A{ abstract function car(); } class TATA extends A{ function car(){ echo “car brand is: TATA”; } } class MAHINDRA extends A{ function car(){ echo “<br>car brand is: mahindra”; } } $nexas = new TATA(); $nexas->car(); $xuv = new MAHINDRA(); $xuv->car(); ?> |

- Abstract class & protected keyword
Working of protected modifiers in PHP: Like the private access modifier, we can also use protected for restricting the usage and accessing of class functions and variables outside of the class. But one exception of protected from private variables is that they can be accessed through inheritance from its parent class in a subclass.
File – code3.php
<?php abstract class A{ abstract protected function car(); } class TATA extends A{ function car(){ echo “car brand is: TATA”; } } $nexas = new TATA(); $nexas->car(); ?> |
