PHP instance Tutorial: abstract class and abstract method

Source: Internet
Author: User
Tags php class example
: This article mainly introduces the PHP Class example Tutorial: abstract class and abstract method. if you are interested in the PHP class, refer to it. Abstract can be used to modify a class or method.

Abstract: indicates that this class is an abstract class. abstract: indicates that this method is an abstract method.

Abstract classes cannot be instantiated.

Abstract methods are implemented only by method declarations, but not by methods.

Abstract class

Abstract can be used to modify a class.

The class modified in abstract indicates that this class is an abstract class.

Abstract classes cannot be instantiated.

This is a simple abstract method. if it is directly instantiated, the system will report an error.

 
The NormalUser in the following example inherits from the User class and can be instantiated.
 
It is meaningless to set an abstract class separately. only with abstract methods can abstract classes have flesh and blood. The following describes the abstract method.

Abstract method

The class modified in abstract indicates that this method is an abstract method.

Abstract methods, only the declaration part of the method, no method body.

The abstract method is used instead.

In a class, as long as there is an abstract method, this class must be declared as an abstract class.

Abstract methods must be overwritten in subclasses.

The following is an abstract class with two abstract methods: setSal () and getSal (). Used to retrieve the salary of $ sal employees.

 
Since the User class cannot be directly inherited, we write a NormalUser class that inherits from the User class. When we write the following code, the system reports an error. This error tells us that there are two abstract methods in the User class, and we must rewrite these two methods in the subclass.
  
In the following example, the two methods are rewritten. although the content of {} in the method body is empty, this method is also overwritten. Note that the parameter names of the rewrite method can be the same as long as the number of parameters is the same, and the parameter names are not required to be the same.
 
In the following lines 19-21, an error is reported in three write override methods.

19 rows. the parameter is missing.

20 rows, more parameters.

Line 21. the parameter type is incorrect. (This method will be introduced in later chapters)

In a class, if there is an abstract method, this class must be declared as an abstract class.

The following class is not an abstract class. If an abstract method is defined, an error is returned.

 

Abstract classes inherit abstract classes

When an abstract class inherits from another abstract class, you do not need to override the abstract method.

Abstract classes cannot overwrite abstract methods of abstract parent classes.

This usage can be understood as an extension of the abstract class.

The following example demonstrates that when an abstract class inherits from another abstract class, you do not need to override the abstract method.

   

After an abstract class is inherited, its abstract methods cannot be overwritten.

If a rewrite occurs, the system reports an error.

 

The abstract class inherits the abstract class to extend the abstract class.

 

In PHP5.1, abstract classes support static abstract methods. The following example shows that static abstract methods can be declared. This method must be a static method.

Static abstract method

In PHP5.1, abstract classes support static abstract methods. The following example shows that static abstract methods can be declared. This method must be a static method.

 // The abstract method here seems to be okay

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.