Home > Article > Backend Development > The difference between static, final, interface and abstract in php
This article mainly introduces the differences between static, final, interface and abstract in PHP. It has certain reference value. Now I share it with you. Friends in need can refer to it
If a method in the parent class is declared final, the subclass cannot override the method. If a class is declared final, it cannot be inherited.
Note: Properties cannot be defined as final, only classes and methods can be defined as final.
Static variables can be local variables or global variables. When a program segment is executed, the static variable does not disappear. It still exists in the memory and will be defined next time. It is still the previous value. It is often used to retain the previous value in recursion or sub-functions. It can be used to define variables and methods; this is also used in the singleton mode;
1. Generally, static attributes are used to save classes Public data
2. Only static properties can be accessed inside static methods, including those of this class and the parent class
3. Static members can be accessed without instantiating objects
4. Use the self or static keyword to access static properties within this class. The variables followed must include $, such as: self::$a or static::$a
5. Access the parent class Use parent for static attributes, such as: parent::$name
6. When accessing static variables or methods outside the class, use the class name to access them directly without instantiation. Such as: Me::$pan and Me::abc()
Abstract classabstract class
1. An abstract class refers to a class with the abstract keyword added before class and an abstract method (the abstract keyword added before the class method function keyword).
2 . Abstract classes cannot be instantiated directly. The abstract class only defines (or partially implements) the methods required by the subclass. Subclasses can make an abstract class concrete by inheriting it and by implementing all the abstract methods in the abstract class.
3. If a subclass needs to be instantiated, it must implement all abstract methods in the abstract class. If the subclass does not implement all the abstract methods in the abstract class, then the subclass is also an abstract class and must be preceded by the abstract keyword in class and cannot be instantiated.
4. If the subclass implements an abstract method, then the access control of the abstract method in the subclass cannot be stricter than the access control of the abstract method in the parent class, that is to say (A parent class, B subclass)
(1 ) If abstract_func() in A is declared as 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 B The declaration of abstract_func() in A can be public or protected, but cannot be private
(3) If abstract_func() in A is declared as private, it cannot be defined as private! ( Fatal error : Abstract function A::abstract_func() cannot be declared private )
1. Abstract classes provide standards for concrete implementation, while interfaces are pure templates. Interfaces only define functions, not implementation content. Interfaces are declared with the keyword interface.
2 . Interface is completely abstract. It can only declare methods, and only public methods. It cannot declare private and protected methods, cannot define method bodies, and cannot declare instance variables.
3. Interface can declare constant variables. But placing constant variables in interface violates the purpose of its existence as an interface, and also confuses the different values of interface and classes. If you really need it, you can put it in the corresponding abstract class or Class.
4. Any class that implements an interface must implement all methods defined in the interface, otherwise the class must be declared abstract.
5. A class can implement an interface using the implements keyword in its declaration. After doing this, the specific process of implementing the interface is the same as inheriting an abstract class that only contains abstract methods.
6. A class can inherit a parent class and implement any number of interfaces at the same time. The extends clause should come before the implements clause. PHP only supports inheritance from one parent class, so the extends keyword can only be followed by a class name.
7. An interface cannot implement another interface, but it can inherit multiple
1. Similarities:
(1) Both It is an abstract class and cannot be instantiated.
(2) Both the interface implementation class and the subclasses of abstract class must implement the declared abstract methods.
2. Differences:
(1) Interface needs to be implemented, using implements , and abstract class needs to be inherited, using extends .
(2) A class can implement multiple interfaces, but a class can only inherit one abstract class.
(3) Interface emphasizes the implementation of specific functions, while abstract class emphasizes the ownership relationship.
(4) Although the interface implementation class and the subclasses of abstract class must implement the corresponding abstract method, the implementation forms are different. Every method in interface is an abstract method, which is only declared (declaration, no method body), and the implementation class must implement it. Subclasses of abstract class can be implemented selectively.
This choice has two meanings:
a) Not all methods in abstract class are abstract. Only those methods with abstract are abstract and subclasses must implement them. For those methods without abstract, the method body must be defined in abstract class;
b) When a subclass of abstract class inherits it, it can directly inherit or override non-abstract methods; while for abstract methods , you can choose to implement it, or you can leave it to its subclasses, but this class must also be declared as an abstract class. Since it is an abstract class, of course it cannot be instantiated.
(5) Abstract class is the 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 declare abstract methods to standardize the functions that subclasses must implement;
On the other hand, it can define a default method body for Subclasses use it directly or override it. In addition, it can define its own instance variables for use by subclasses through inheritance.
(6) The abstract keyword cannot be added before the abstract method in the interface. The abstract method is implicit by default, and the final keyword cannot be added to prevent the inheritance of abstract methods. In an abstract class, abstract must be added before the abstract method to indicate that it is explicitly declared as an abstract method.
(7) The abstract methods in the interface are public by default and can only be public. They cannot be modified with private or protected modifiers. Abstract methods in abstract classes can be modified with public and protected, but cannot be modified with private.
3. Application occasions of interface
(1) Classes need specific interfaces for coordination, regardless of how they are implemented.
(2) It exists as an identifier that can realize a specific function, or it can be a pure identifier without any interface methods.
(3) It is necessary to treat a group of classes as a single class, and the caller only contacts this group of classes through the interface.
(4) It is necessary to implement multiple specific functions, and these functions may have no connection at all.
4. Application occasions of abstract class
In a word, you can use it when you need both a unified interface and instance variables or default methods. The most common ones are:
(1) Define a set of interfaces, but do not want to force each implementation class to implement all interfaces. You can use abstract class to define a set of method bodies, or even empty method bodies, and then let subclasses choose the methods they are interested in to cover.
(2) In some cases, pure interfaces alone cannot satisfy the coordination between classes. Variables representing states in the classes are also needed to distinguish different relationships. The intermediary role of abstract can satisfy this very well.
(3) Standardizes a set of mutually coordinated methods, some of which are common, independent of state, and can be shared without the need for subclasses to implement them separately; while other methods require each subclass to implement them according to own specific state to achieve specific functions.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
The difference between Define and Const in PHP
The above is the detailed content of The difference between static, final, interface and abstract in php. For more information, please follow other related articles on the PHP Chinese website!