PHP OOP - Abstract Classes

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
<?php
// Abstract class
abstract class Vehicle {
protected $brand;
protected $speed;
// Constructor
public function __construct($brand, $speed) {
$this->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
<?php
// Abstract class
abstract class Vehicle {
protected $brand;
// Constructor
public function __construct($brand) {
$this->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
<?php
// Abstract class
abstract class Vehicle {
protected $brand;
// Constructor
public function __construct($brand) {
$this->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 "<br>";
echo $myCar->startEngine("Physical", "Half", "Sport");
// Output: Starting the Toyota car with a Physical key, Fuel Level: Half, Mode: Sport.
?>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.