PHP abstract class has at least one abstract method and a non-abstract method and the declared abstract method can not be implemented inside the abstract class. Implementation of the abstract method can be done inside the child class.
Please keep in mind that the declared abstract method inside the child class will have some visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. An abstract method does not have a body hence curry braces {}
is not used.
Note:Please keep in mind that an abstract class or abstract method both are defined with the abstract
keyword.
The abstract
keyword is used to define the abstract class. It places just before the class
keyword.
abstract class SuperClass {
//write abstract and none abstract method
}
An abstract method must be declared inside the abstract class by just prepending abstract
to the method.The abstract method does not have implementation code hence no requirement to use curly braces {}
.
The abstract method's visibility will be either public or protected, but not private.
abstract class SuperClass {
abstract public function userDefinedMethod1();
abstract protected function userDefinedMethod2($name, $age);
abstract protected function userDefinedMethod3() : int;
}
Abstract class can also have a none abstract method. This method can be accessed by the child class. Please keep in mind that abstract classes can not be instantiated.
abstract class SuperClass {
abstract public function myMethod1();
public function myMethod2() {
echo "Hello World";
}
}
Please keep in mind that objects can not be created from abstract classes.
abstract class SuperClass {
abstract public function myMethod1();
public function myMethod2() {
echo "Hello World";
}
}
$obj = new SuperClass;//invalid syantx
The visibility of the child's method will be similar or less restricted in comparison to the parent's method.
Abstract Method's Visibility | Child Method's Visibility |
---|---|
Public | Public |
Protected | Protected or public |
abstract class SuperClass{
abstract public function myMethod1();
abstract protected function myMethod2();
abstract protected function myMethod3();
public function myMethod2() {
echo "Hello World";
}
}
class ChildClass extends SuperClass{
abstract public function myMethod1(){
//write source of code
}
abstract protected function myMethod2(){
//write source of code
}
abstract public function myMethod3(){
}
}
Abstract method declared inside the abstract class should not contain a private access modifier.
abstract class SuperClass {
abstract private function myMethod1();//invalid due to private access modifier
}
The arguments for child class methods should be the same as the abstract method of the abstract class.
public function userDefinedMethod($name, $age) {...}
Child class might have a default argument while the abstract class method does not have an argument with a default value.
public function myMethod2($name, $age, $country = 'USA') {...}
Let us understand abstract class with the help of examples.
abstract class Person {
public $name;
public function __construct($name) {
$this -> name = $name;
}
abstract public function greet() : string;
}
class Programmer extends Person {
public function greet() : string {
return "Hello World from " . $this -> name;
}
}
class Student extends Person {
public function greet() : string {
return "Howdy! I'm " . $this -> name;
}
}
class Teacher extends Person {
public function greet() :string {
return "Good morning dear students";
}
}
Since objects can not be created from an abstract class hence to create objects, we will use child classes.
The Objects
$programmer = new Programmer('John');
echo $programmer -> greet();
$student = new Student('Doe');
echo $student -> greet();
$teacher = new Teacher('Mary');
echo $teacher -> greet();
Source Code
<?php
abstract class Person {
public $name;
public function __construct($name) {
$this -> name = $name;
}
abstract public function greet() : string;
}
class Programmer extends Person {
public function greet() : string {
return "Hello World from " . $this -> name;
}
}
class Student extends Person {
public function greet() : string {
return "Howdy! I'm " . $this -> name;
}
}
class Teacher extends Person {
public function greet() :string {
return "Good morning dear students";
}
}
$programmer = new Programmer('John');
echo $programmer -> greet();
echo "<br>";
$student = new Student('Doe');
echo $student -> greet();
echo "<br>";
$teacher = new Teacher('Mary');
echo $teacher -> greet();
echo "<br>";
?>