Abstract methods and abstract class explanations in PHP

Source: Internet
Author: User
This article mainly introduces the abstract methods and abstract classes in PHP. Have a good reference value, follow the small series together to see it

1. What is an abstract method?

The method that we define in the class that has no method to mention is an abstract method. The so-called no method body refers to, at the time of declaration without curly braces and the contents of which, but directly at the declaration of the method name after the end of the semicolon, and in the declaration of the abstract method is also added to the keyword "abstract" to decorate.

For example:

Abstract function fun1 (); Abstract function fun2 ();

2. What is abstract class?

As long as there is one method in a class that is abstract, the class is defined as an abstract class, and the abstract class is also decorated with the "abstract" keyword, and there can be no abstract method and member property in the abstract class, but as long as there is a method that is abstract, the class must be declared as an abstract class, using " Abstract "modifier.

For example:

Abstract class Demo{var $test; abstract function fun1 (); abstract function fun2 ();}

3. What is the role of abstract classes?

The example above defines an abstract class "demo" that uses "abstract" to modify, defines a member attribute "$test" in this class, and two abstract methods "Fun1" and "fun2" have a non-abstract method fun3 (); So how do we use abstract classes? The most important point is that the abstract class can not produce an instance object, so it can not be used directly, we mentioned many times before the class can not be used directly, we use the object through the class instantiation, then the abstract class can not produce an instance object we declare the abstract class what is the use of it? We use the abstract method as a template for subclass overloading, and defining an abstract class is equivalent to defining a specification that requires subclasses to obey, and then the abstract method in the abstract class is implemented according to the needs of the subclass after the subclass class. Subclasses must implement all the abstract methods in the parent class, otherwise there are abstract methods in the subclass, then the subclass is abstract or not, and why should we inherit from the abstract class? Because sometimes we have to implement some functions must inherit from the abstract class, otherwise these functions you can not realize, if inherit the abstract class, it is necessary to implement the abstract method of the class;

For example:

Abstract class mode{var $test; abstract function fun1 (); abstract function fun2 (); function fun2 () {  ...}} $mode = new mode (); An abstract class can only be used to produce an instance object, so it is wrong to instantiate the object to the subclass class Test extends mode{  function fun1 () {  ...  }    function fun2 () {  ...  }} $test = new test (); Subclasses can instantiate an object because it implements the abstract method of all the parent classes

The above is the whole content of this article, I hope that everyone's study has helped.


Related Article

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.