PHP abstract methods, abstract classes, and interfaces

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 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 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

4. What is an interface (interface)?

Interfaces are abstractions of methods, and if different classes have the same method, then you should consider using interfaces.
(1) An interface is a specification and protocol for a behavior. is actually a kind of agreement between classes and classes, a constraint
(2) C # does not support multiple inheritance, but he gives this function to the interface to implement.
(3) The system resource calls between classes and classes are different, resulting in difficult communication between them, and interfaces can mask the differences between them, enabling them to communicate smoothly.

5. Interfaces and abstract classes

A class can inherit multiple interfaces ...
A class can inherit only one abstract class ...

An abstract method is a method that must be implemented. It's like the animals have to breathe. But fish breathe with their gills, and pigs breathe with their lungs.
Animals must have breathing methods. How to breathe is a subclass thing.

Now there are many discussions and suggestions to promote the use of interface instead of the abstract class, both theoretically can do general mixing, but in practical applications, they still have a certain difference. Abstract classes generally provide the basis for the extension of a subclass as a common parent class, where extensions include both property and behavior. while interfaces generally do not consider attributes, only methods are considered so that subclasses can freely fill or extend the method defined by the interface, just as the adapter in the event of the Java Prince says is a good application.
With a simple example, for example, a teacher, we use it as an abstract class, has its own attributes, such as age, education, teacher number, etc., and teachers are divided into many categories, we can inherit the teacher class and expand the unique category attributes, and the universal attributes have been directly inherited down.
And the interface is to take the teacher as an example, the teacher's behavior is many, in addition to the same as ordinary people, there are career-related behaviors, such as changing the examination papers, lectures, etc., we define these behaviors as a non-body method, as a set, it is a interface. and teachers Dick and harry their own behavioral characteristics are different, then they can expand their behavior body. In this sense, interface isbiased towards behavior .
In short, in many cases, interfaces can indeed be substituted for abstract classes if you do not need to deliberately express inheritance on attributes.

The interface defines a general specification for implementing a service, declares the required functions and constants, but does not specify how to implement them. The details of the implementation are not given because different entities may need to implement a common method definition in different ways. The key is to establish a set of general principles that must be achieved, and only if these principles are met can the interface be realized.

The class member is not defined in the interface! The definition of a class member is completely given to the implementation class.

PHP abstract methods, abstract classes, and interfaces

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.