Abstract Class:
Declared with the abstract keyword.
Cannot be instantiated directly.
Can include both abstract methods (without implementation) and concrete methods (with implementation).
Abstract Method:
Declared in an abstract class with the abstract keyword.
Does not have a body (no implementation).
Must be implemented in any child class that inherits the abstract class.
Rules for Abstract Classes:
The child class must implement all abstract methods of the parent class.
Abstract methods in the child class must have the same name, visibility, and required arguments (optional arguments can be added).
abstract class ParentClass { abstract public function abstractMethod1(); abstract public function abstractMethod2($param); abstract public function abstractMethod3(): string; }
Example 1: Implementing Abstract Methods
<?php // Parent abstract class abstract class Vehicle { public $brand; public function __construct($brand) { $this->brand = $brand; } // Abstract method abstract public function getDetails(): string; } // Child class 1 class Car extends Vehicle { public function getDetails(): string { return "This is a car of brand $this->brand."; } } // Child class 2 class Bike extends Vehicle { public function getDetails(): string { return "This is a bike of brand $this->brand."; } } // Instantiate child classes $car = new Car("Toyota"); echo $car->getDetails(); // Outputs: This is a car of brand Toyota. echo "<br>"; $bike = new Bike("Yamaha"); echo $bike->getDetails(); // Outputs: This is a bike of brand Yamaha. ?>
Example 2: Abstract Methods with Arguments
<?php abstract class Shape { abstract public function area($length, $width): float; } class Rectangle extends Shape { public function area($length, $width): float { return $length * $width; } } class Triangle extends Shape { public function area($length, $width): float { return 0.5 * $length * $width; } } $rectangle = new Rectangle(); echo "Area of Rectangle: " . $rectangle->area(10, 5) . "<br>"; // Outputs: Area of Rectangle: 50 $triangle = new Triangle(); echo "Area of Triangle: " . $triangle->area(10, 5) . "<br>"; // Outputs: Area of Triangle: 25 ?>
Example 3: Abstract Method with Optional Arguments in Child Class
<?php abstract class Greeting { abstract protected function sayHello($name); } class FriendlyGreeting extends Greeting { public function sayHello($name, $greeting = "Hello") { return "$greeting, $name!"; } } $greeting = new FriendlyGreeting(); echo $greeting->sayHello("Alice"); // Outputs: Hello, Alice! echo "<br>"; echo $greeting->sayHello("Bob", "Hi"); // Outputs: Hi, Bob! ?>
Advantages of Abstract Classes
Code Reusability:
Common functionality can be defined in the abstract class, and specific behaviors can be implemented in child classes.
Enforce a Structure:
Child classes must follow the contract set by the abstract class by implementing all abstract methods.
Flexibility:
Concrete methods in abstract classes can provide shared logic, reducing code duplication.
Comparison with Interfaces
Aspect Abstract Class Interface
Instantiation Cannot be instantiated Cannot be instantiated
Methods Can have abstract and concrete methods All methods are abstract
Properties Can have properties Cannot have properties
Multiple Inheritance A class can extend only one abstract class A class can implement multiple interfaces
Conclusion
Abstract classes and methods provide a foundation for creating structured, reusable, and extendable code in PHP. They allow developers to enforce specific behaviors in child classes while maintaining flexibility for implementation details.