Abstract Class in PHP

Tram Ho

➔ PHP 5 incorporates the concept of abstract classes and abstract methods.

➔ The abstract class defined is not intended to be used to create an object from it; the abstract class has at least one abstract method that must be initialized to mean an abstract class.

➔ Abstract methods only declare the method name and parameter list (method signature), but do not define the inner executable (nobody like interface methods).

Inherit the abstract class

➔ When a class inherits an abstract class, it must define (implement) all abstract methods in the abstract class that it inherits.

➔ In addition, methods must have the same visibility level defined or higher than the specified visibility in the parent class (public, private, protected)

Example

➔ AbstractClass abstract class definition

➔ Creates a ConcreteClass1 child class inheriting AbstractClass

➔ Creates a ConcreteClass2 child class inheriting AbstractClass

➔ Creates $class1 $class2 object

➔ Show result

Share the news now

Source : Viblo