PHP Abstract Classes


PHP - What are Abstract Classes and Methods?

An abstract classes are incomplete classes.

A abstract class can contains method implementation and also method declaration. The declared method must be implemented by a child class.

We cannot create a object for an abstract class.

Whenever a parent class contains named methods, it relies on its child classes to complete the tasks.

A class that has at least one abstract method is considered abstract. The code for an abstract method does not contain any implementation.

With the abstract keyword, you can create an abstract class or method :

Syntax :-

<?php
abstract class ParentClass {
  abstract public function someMethod1();
  abstract public function someMethod2($name, $color);
  abstract public function someMethod3() : string;
}
?>

Child class methods must have the same names and access modifiers as the parent class methods when inheriting from an abstract class. So, if the abstract method is protected, the child class method must be protected or public, but not private. Determining the type and amount of needed arguments must also be identical. The child classes, on the other hand, may also accept optional arguments.

As a result, we have the following restrictions when a child class inherits from an abstract class :

  • Similarly, the abstract method of the child class must be re-declared in the child class method.
  • The access modifier for the child class method must be the same or less limited.
  • The amount of arguments necessary must remain constant. Optional parameters can be added to the child class, though.

Example :-

<?php
// Parent class

abstract class Car {
  public $name;

  public function __construct($name) {
    $this->name = $name;
  }

  abstract public function intro(); 
}

// Child classes

class Audi extends Car {
  public function intro() {
    echo "<br />Choose German quality! I'm an $this->name!"; 
  }
}

class Volvo extends Car {
  public function intro() {
    echo "<br />Proud to be Swedish! I'm a $this->name!"; 
  }
}

// Create objects from the child classes

$audi = new audi("Audi");
echo $audi->intro();

$volvo = new volvo("Volvo");
echo $volvo->intro();
?>

Output :-

Choose German quality! I'm an Audi!
Proud to be Swedish! I'm a Volvo!

Example Explained :-

The Car class is passed down to the Audi and Volvo classes. Because of inheritance, the public $name field and the public __construct() function from the Car class are available to the Audi and Volvo classes.

The intro() method must be implemented because it is an abstract method that should be defined in all child classes.

You can also search for these topics, PHP OOP Abstract Classes, php oop abstract keyword, parent class and child class using php oop, use of names methods in php oop, syntax for php oop abstract classes, Example for php oop abstract classes, php oop rules to access abstract classes, PHP More Abstract Class Examples, define the abstract class arguments in php oop.