Brief discussion on abstract and interface interface of PHP abstraction class

Source: Internet
Author: User
Tags what interface

I. Specification for abstract class definitions

1. PHP5 supports abstract classes and abstract methods

2, the definition of abstract class can not be instantiated

3. If an abstract method is declared in any of the classes, the class must be declared as an abstract class

4, is defined by the abstract method can only declare its invocation method (parameters), can not define its specific function implementation

5, continue the subclass of the abstract class must define the parent class (abstract class) all the abstract methods, and access control (public, protected) must be consistent, or more lenient such as: The parent class protected, subclasses can protected, public. If the parent class is public, then the subclass can only be public, not protected

6. Abstract class access control cannot define private

7. Abstract classes can define non-abstract methods

8. Abstract classes can define attributes

code example
<?phpabstract class userabstract{protected $value 1 = 0;      Private $value 2 = 1;      Public $value 3 = 2;  Defines a protected abstract protected function register ();//abstract Public Function login ()// Cannot define an abstract method of a private property//abstract private login ();p rotected function Read () {return $this->value2;}} /*** */class ClassName extends userabstract{public function login () {echo ' This is a login ';} Public Function Register () {echo ' This is a registration ';} Public Function visit () {//Access abstract class Property echo $this->value3;//private property cannot access//echo $this->value2;//Access abstract class protected property echo $ this->value1;//Access abstract protected non-static method echo $this->read ();}} $obj = new ClassName (); $obj->login (); $obj->register (); $obj->visit ();

  

Interface (interface) interface Specification

1. Interfaces can be used to specify which methods are implemented by some classes, but interfaces do not need to define the specific contents of these methods

2, the interface is defined by the interface keyword, as defined by a standard class, but the definition of all methods are empty.

3. All methods defined in the interface must be public, and this is the attribute of the interface.

4. To implement an interface, use the implements operator. A class can implement multiple interfaces, separating the names of multiple interfaces with commas.

5, when implementing multiple interfaces, the methods in the interface cannot have duplicate names.

6. Interfaces can also be inherited by using the extends operator.

7. To implement an interface, a class must be in a way that is completely consistent with the methods defined in the interface. Failure to do so can result in fatal errors.

8, the interface can also define constants. Interface constants and class constants are used exactly the same, but cannot be overridden by subclasses or sub-interfaces.

code example
<?phpinterface itemplate{public Function Login ($user, $pwd);p ublic function Register ($user, $pwd);} Interface Itemplateext extends Itemplate{const read = ' read ';p ublic function READ ($content);} Interface ITemplate2 {public function goods ();} /*** */class ClassName implements Itemplateext,itemplate2{public function login ($user, $pwd) {echo ' login ';} Public Function Register ($user, $pwd) {echo ' This is a registration ';} Public function Read ($read) {echo ' read ';//echo Self::read;} Public Function Goods () {echo ' Product ';}} $obj = new ClassName (), $obj->login (' name ', ' pwd '), $obj->register (' name ', ' pwd '), $obj->read (' pwd '); $obj- >goods (); Echo classname::read;

  

The difference between interface and abstraction and the same point, application Scenario 1, the same point

1, both are abstract classes, can not be instantiated.

2. The subclass of interface implementation class and abstract class must implement an abstract method that has already been declared.

2. Difference

1, interface need to realize, to use implements, and abstract class need to inherit, to use extends.

2. A class can implement multiple interface, but a class may inherit only one abstract class.

3. Interface emphasizes the realization of specific functions, while abstract class emphasizes the affiliation. (Difference of the function)

4, although interface implementation class and abstract class subclass must implement the corresponding abstract method, but the form of implementation is different. Each method in interface is an abstract method, all just declared (declaration, no method body), the implementation class must be implemented. The subclass of abstract class can be implemented selectively. This option has a two point meaning: a) Not all methods in abstract class are abstract, only those whose crowns have abstractions are abstract and must be implemented by subclasses. Those that have no abstract method, must define the method body in the abstract class, B) The subclass of abstract class inherits it, the non-abstract method can be either directly inherited or overwritten, and the abstract method can be chosen to implement, but also left to its subclasses to implement, However, this class must also be declared as an abstract class. Abstract classes, and of course, cannot be instantiated.

5. Abstract class is an intermediary between interface and class. Abstract class plays a connecting role in interface and class. On the one hand, abstract class is abstract and can be used to declare abstractions to standardize the functions that subclasses must implement, on the other hand, it can define the default method body for the subclasses to use or overwrite directly. In addition, it can define its own instance variables for use by subclasses through inheritance.

6, the interface in the abstract method before you can not add the abstract keyword, the default implicit is an abstract method, you can not add the final keyword to prevent the inheritance of abstract methods. Abstract methods in abstract classes must be preceded by an abstract representation to display the declaration as an abstraction.

7, the interface of the abstract method is public by default, can only be public, can not be modified with private, protected modifier. Abstract methods in abstract classes can be modified with public, protected, but not private.

3, the Application scenario interface application occasions

1. There is a need for a specific interface between classes and classes that do not care about how they are implemented.

2, as the ability to achieve a specific function of the identity exists, can also be what interface methods do not have a pure identity.

3. A group of classes needs to be treated as a single class, and the caller is only connected to this set of classes through an interface.

4. Need to implement a specific number of functions, and these features may be completely without any connection.

5. Specification for the implementation of the specified class

6. Forcing a class to implement a method

Application of abstract class

1, when the complete class implementation is not expected, you can use the abstract class to implement

2, define a set of interfaces, but do not want to force each implementation class must implement all the interfaces

3, in some cases, only by the pure interface can not meet the class and the coordination between classes, but also necessary to represent the state of the variables in the class to distinguish between different relationships. Abstract mediation can be a good way to meet this point.

4. Specification for the implementation of the specified class

A brief talk on the application scenario between 2 people

Personal use of the method: When the interface can meet the requirements of the interface can be used, when the interface is not satisfied, it is necessary to use the abstract class.

Brief discussion on abstract and interface interface of PHP abstraction class

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.