Introduction to PHP Object-oriented (interface) basic concepts and abstract class examples

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

(4) You can specify which methods a class must implement, but you do not need to define the specific contents of those methods.

What is abstract class (AbstractClass)?

1. An abstract class provides only a partial implementation of a type. An abstract class can have an instance variable, and one or more constructors. Abstract classes can have both abstract methods and concrete methods. An abstract class does not have an instance, and these constructors cannot be called by the client to create an instance. The constructor of an abstract class can be called by its subclasses, so that all subclasses of an abstract class can have some common implementations, and different subclasses can have their own implementations on this basis.

2. Purpose of abstract class 1) a specific class is not used to inherit. Scottmeyers has pointed out that, whenever possible, do not inherit from specific classes. 2) There are 2 specific classes, Class A and Class B, Class B is a subclass of Class A, then one of the simplest modification scenarios is to create an abstract class (or Java interface) C, and then make Class A and Class B a subclass of abstract class C. 3) Abstract classes should have as much common code as possible. To improve the code reuse rate. 4) Abstract classes should have as little data as possible.

3. Abstract class-based patterns and principles 1) for abstract programming, do not target specific programming. 2) use synthesis (composition) as much as possible, rather than using inheritance to achieve reuse. 3) using the Trackpad method mode

4. When should you use Inheritance Multiplexing 1) subclasses are a special kind of superclass, not a role of superclass, that is, to differentiate between "is–a" and "has-a". 2) There is never a case where a subclass needs to be replaced by another subclass. If the designer is not sure that a class will not become a subclass of another class in the future, then this class should not be designed as a subclass of this superclass. 3) Subclasses have the responsibility of extending the superclass, rather than having the responsibility of displacing (Override) or unregistering (nullify) the superclass. 4) You can use inheritance only if there is a taxonomic significance, and do not inherit from the tool class.

An abstract method is a method that must be implemented. And can only be in an abstract class.

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 is biased 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.

Inherit more interfaces

<?php  Interface  a{public      function  foo ();  }  Interface  b{public      function  Bar ();  An interface class can inherit multiple interfaces  interface  C  extends  A,  b{public      function  baz ();  }    Class  D  implements  C  {public      function  foo () {      } public      function  Bar () {      } public      function  Baz () {      }  }  ?>

In PHP, not only a class can implement multiple interfaces, but also can inherit a class at the same time to implement multiple interfaces, it is important to inherit the class to implement the interface

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.