The difference between static, final, interface, and abstract in PHP

Source: Internet
Author: User
Tags what interface
This article mainly introduces the static, final, interface, abstract of the difference in PHP, has a certain reference value, now share to everyone, the need for friends can refer to

Final

If a method in the parent class is declared final, the child class cannot overwrite the method. If a class is declared final, it cannot be inherited.

Note: attributes cannot be defined as final, only classes and methods can be defined as final.

Static

A static variable can be a local variable or a global variable, when a program segment is completed, the static variable does not disappear, it still exists in memory, the next time in the definition or the previous value, often used in recursive or sub-function to retain the previous value, can be used to define variables and methods, the singleton mode is also used this;

1. General static properties are used to hold the class's public data

2, static methods can only access static properties, including the class and the parent class

3, static members do not need to instantiate the object can be accessed

4. Accessing static properties Inside this class is accessed with the self or the static keyword, and the variables on the back include $, for example: self: $a or static:: $a

5. Access the parent class static property using parent, such as: Parent:: $name

6. Access static variables or methods outside the class using the class name directly, without instantiation. such as: Me:: $pan and Me::abc ()

Abstract

Abstract class

1. An abstract class is a class that has an abstract keyword in front of the class and an abstraction method (which adds the abstract keyword before the class method function keyword).

2. Abstract classes cannot be instantiated directly. Only the methods required for defining (or partially implementing) subclasses in an abstract class. Subclasses can make abstract classes materialized by inheriting abstract classes and by implementing all the abstract methods in the abstract class.

3. If a subclass needs to be instantiated, it implements all the abstract methods in the abstract class. If the subclass does not fully implement all the abstract methods in the abstract class, then the subclass is an abstract class that must precede the class with the abstract keyword and cannot be instantiated.

4. If the subclass implements an abstract method, the access control of the abstract method in the subclass cannot be more restrictive than the abstract method access control in the parent class, i.e. (a parent class, B subclass)

(1) if Abstract_func () in A is declared public, then the declaration of Abstract_func () in B can only be public, not protected or private

(2) if Abstract_func () in A is declared as protected, then the declaration of Abstract_func () in B can be public or protected, but not private

(3) if Abstract_func () in A is declared private, it cannot be defined as private! ( Fatal error : Abstract function A::abstract_func () cannot be declared private)

Interface

1. Abstract classes provide a concrete implementation of the standard, and the interface is purely a template. The interface defines only the functionality, not the content of the implementation. The interface is declared with the keyword interface.

2. Interface are completely abstract, can only declare methods, and can only declare methods of public, cannot declare private and protected methods, cannot define method bodies, and cannot declare instance variables.

3. Interface can declare constant variables. But putting constant variable in interface violates its purpose of being an interface, and it confuses the different value of interface and class. If you really need it, you can put it in the appropriate abstract class or class.

4. Any class that implements an interface implements all of the methods defined in the interface, otherwise the class must be declared abstract.

5. A class can use the Implements keyword in a declaration to implement an interface. After doing so, the exact process of implementing the interface is the same as inheriting an abstract class that contains only abstract methods.

6. A class can inherit a parent class and implement any number of interfaces at the same time. The extends clause should precede the implements clause. PHP only supports inheriting from a parent class, so the extends keyword can only be followed by a class name.

7. Interfaces can not implement another interface, but can inherit multiple

Iii. similarities and differences of abstract classes and interfaces

1. The same point:

(1) Both are abstract classes and cannot be instantiated.

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

2. Different points:

(1) interface need to implement, to use implements, and abstract class need to inherit, to use extends.

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

(3) interface emphasizes the realization of a particular function, while the abstract class emphasizes its affiliation.

(4) Although the subclass of the interface implementation class and the abstract class must implement the corresponding abstract method, the implementation is different in form. 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 implication:

A) Not all of the methods in abstract class are abstract, only those whose crown has an abstract are abstracted, and subclasses must be implemented. Those methods that are not abstract must define the method body in the abstract class;

b) When the subclass of the abstract class inherits from it, the non-abstract method can either be inherited directly or overwritten, whereas for an abstract method, you can choose to implement it or leave it to its subclasses, but 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 one hand, abstract class is abstract and can be used to declare abstraction methods to standardize the functions that subclasses must implement;

On the other hand, it can define the default method body for the subclass to use or overwrite directly. In addition, it can define its own instance variables for use by subclasses through inheritance.

(6) Before the abstract method in the interface can not add the abstract keyword, the default implicit is an abstract method, and 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 abstract method in the interface is public by default, and can only be public, and cannot be decorated with the private, protected modifier. Abstract methods in abstract classes can be modified with public, protected, but not private.

3. Interface applications

(1) Classes and classes require a specific interface to reconcile, regardless of how they are implemented.

(2) as an identity that is capable of achieving a specific function, it can also be a purely identifier of what interface method does not exist.

(3) A set 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) A specific number of functions need to be implemented, and there may be no connection between these features at all.

4. Application of abstract class

In a word, it can be used in situations where both a uniform interface is required and an instance variable or a default method is required. The most common are:

(1) Defines a set of interfaces, but does not want to force each implementation class to implement all interfaces. You can use the abstract class to define a set of method bodies, even an empty method body, and then select the methods that you are interested in to overwrite by subclasses.

(2) On some occasions, purely interfaces do not satisfy the coordination between classes and classes, and the variables that represent states in a class must differentiate between different relationships. Abstract mediation can be a good way to meet this point.

(3) standardize a set of mutually coordinated methods, some of which are common, state-independent, shareable, without subclasses, and others require individual subclasses to implement specific functions according to their specific state.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.