How do you understand abstract classes and abstract methods in PHP? (Attached code)

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.