Advertisement

Google Ad Slot: content-top

PHP OOP - Abstract Classes

~1 min read · PHP
On this page

PHP - What are Abstract Classes and Methods?


An abstract class in PHP is a class that cannot be instantiated directly. This means you can't create objects straight from an abstract class.


An abstract class is a class that contains at least one abstract method and it does not have body. The body is provided by the subclass (inherited from).


An abstract class or method is defined with the abstract keyword:


Key Features of Abstract Classes:

✔ Declared using the abstract keyword.

✔ Cannot be instantiated directly.

✔ Can contain both abstract and regular methods.

✔ Child classes must implement all abstract methods.


Syntax


abstract class ClassName {
  abstract public function methodName(); // Abstract method (must be implemented)
   
  public function regularMethod() {
    // Regular method (optional to override)
  }
}



Rules for Implementing Abstract Methods in Child Classes

  1. Same Method Name
  • The child class must define the method with the exact same name as in the abstract class.
  1. Access Modifier Restriction
  • The child class method cannot have a more restricted access modifier than the abstract method.
  • If an abstract method is protected, the child class method can be protected or public, but not private.
  1. Same Number of Required Arguments
  • The child class must implement the abstract method with the same number of required parameters.
  • However, the child class can add optional parameters.


Let's look at an example:

Example
brand = $brand; $this->speed = $speed; } // Abstract method (must be implemented in child class) abstract public function startEngine(); } // Concrete class that extends the abstract class class Car extends Vehicle { // Implementing the abstract method public function startEngine() { return "Starting the engine of the $this->brand car!"; } } // Creating an object of the Car class $myCar = new Car("Toyota", 180); // Calling methods echo $myCar->startEngine(); // Output: Starting the engine of the Toyota car! ?>
Try it yourself

Explanation:

1. Vehicle (Abstract Class)

  • Defines common properties (brand, speed).
  • Contains an abstract method startEngine() (to be implemented by child classes).


2. Car (Concrete Class)

  • Extends Vehicle and implements startEngine().


3. Creating an Object

  • Car is instantiated and methods are called.

PHP - More Abstract Class Examples

Let's look at another example where the abstract method has an argument:

Example
brand = $brand; } // Abstract method with a parameter abstract public function startEngine($keyType); } // Concrete class that extends the abstract class class Car extends Vehicle { // Implementing the abstract method with a parameter public function startEngine($keyType) { return "Starting the engine of the $this->brand car using a $keyType key!"; } } // Creating an object of the Car class $myCar = new Car("Toyota"); // Calling methods echo $myCar->startEngine("Smart"); // Output: Starting the engine of the Toyota car using a Smart key! ?>
Try it yourself

In this example, we'll define an abstract class Vehicle with an abstract method startEngine($keyType), where:

  • The abstract method has one required parameter ($keyType).
  • The child class Car adds two optional parameters ($fuelLevel, $engineMode).
Example
brand = $brand; } // Abstract method with one required parameter abstract public function startEngine($keyType); } // Concrete class that extends the abstract class class Car extends Vehicle { // Implementing the abstract method with additional optional parameters public function startEngine($keyType, $fuelLevel = "Full", $engineMode = "Normal") { return "Starting the $this->brand car with a $keyType key, Fuel Level: $fuelLevel, Mode: $engineMode."; } } // Creating an object of the Car class $myCar = new Car("Toyota"); // Calling methods with different arguments echo $myCar->startEngine("Smart"); // Output: Starting the Toyota car with a Smart key, Fuel Level: Full, Mode: Normal. echo "
"; echo $myCar->startEngine("Physical", "Half", "Sport"); // Output: Starting the Toyota car with a Physical key, Fuel Level: Half, Mode: Sport. ?>
Try it yourself