Abstract methods and abstract classes in PHP are simple and straightforward.

Source: Internet
Author: User

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 an abstraction class?        as long as there is one method in a class that is abstract, the class is defined as an abstract class, and abstract classes are also decorated with the "abstract" keyword. There can be no abstract methods and member properties in an abstract class, but as long as one method is an abstract method, the class must be declared as an abstract class, using the "abstract" modifier. For example: abstract class Demo{var $test; abstract function fun1 (); abstract function fun2 ();} What is the role of           3 and abstract classes? The          example defines an abstract class "demo" that uses "abstract" to modify 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 also can not directly make  , we have mentioned many times before the class can not be used directly, we use the object through the class instantiation, then the extraction   like class can not produce an instance object we declare the abstract class what is the use of it? We are using the abstract method as a subclass overload of the module   Board, the definition of abstract class is equivalent to defining a specification, this specification requires subclasses to obey, subclass of the class of the abstract   class, the abstract class inside the abstract method according to the needs of the child class implementation. Subclasses must implement all   of the abstract method in the parent class, otherwise there is an abstract method in the subclass, then the subclass or abstract class, or cannot instantiate the pair; Why do I   have to inherit from an abstract class? Because sometimes we have to implement some functions must inherit from the abstract class, otherwise   these functions you can not implement, 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

Abstract methods and abstract classes in PHP, simple and straightforward, a bit of a pass

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.