Today, we are going to talk about the content of abstract and abstract methods in object-oriented PHP, in a nutshell, the abstract class in PHP is a class that cannot be instantiated, can only be used as the parent class of other classes, and the abstract method cannot take the method body, so let's look at the concrete examples below.
Object-oriented abstract methods and abstract classes:
All subclasses must inherit the methods of the parent class, but the inheritance is nondeterministic and the method of the parent class must be overridden, at which point the overridden method is defined as an abstract method, and the abstract method does not require a method body. This class is also abstract abstruct.
An abstract class is a class that cannot be instantiated and can only be used as a parent class for other classes!
If there is one method in a class that is abstract, then this class is an abstract class.
If a class is an abstract class, the class must use the abstract adornment.
Abstract class is a special class, interface is a special kind of abstract class, polymorphic will use to abstract class or interface!
The test code is as follows:
Abstract class A{public $name = ' zym '; abstract function Show ();} $a = new A ()//Will error class B extends A{public function Show () {echo ' inherits the abstract method of the parent class and rewrites ';}} $b = new B (); $b->show ();
Abstract classes are similar to ordinary classes, and contain member variables and member methods, the difference is that the abstract class must contain at least an abstract method, the abstract method does not have a method body, the implementation of its functions can only be done in the subclass.
The code is as follows:
Define a class named "Transportation" abstract class vehicle{private $name;//name private $price;//price private $brand;//Brand abstract function run ( );} Class Truck extends Vehicle{function run () {echo ' truck running on the road ';}} Class Plane extends Vehicle{public function run () {echo ' plane flies in the sky ';}} $plane = new plane; $plane->run ();
Note: The abstract method must not take the method body!