php interface and abstract class use examples in detail

  • 2021-01-18 06:21:38
  • OfStack

1. Abstract class abstract class

1. An abstract class is a class that has the abstract keyword added before class and has abstract methods (the abstract keyword added before the function class method).

2. An abstract class cannot be instantiated directly. An abstract class defines (or partially implements) only the methods required by a subclass. Subclasses can externalize an abstract class by inheriting it and by implementing all of its abstract methods.

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


abstract class A  
{  
    /**  Variables can be defined in abstract classes  */ 
    protected $value1 = 0;  
    private $value2 = 1;  
    public $value3 = 2;  
    /**  Non-abstract methods can also be defined  */ 
    public function my_print()  
    {  
        echo "hello,world/n";  
    }  
    /** 
     *  In most cases, abstract classes contain at least one 1 An abstract method. Abstract method application abstract Keyword declaration, which cannot have concrete content.  
     *  You can declare an abstract method just as you would a normal class method, but end with a semicolon instead of the method body. That is, abstract methods cannot be implemented in abstract classes, that is, there is no function body." {some codes} ".  
     */ 
    abstract protected function abstract_func1();  
    abstract protected function abstract_func2();  
}  
abstract class B extends A  
{  
    public function abstract_func1()  
    {  
       echo "implement the abstract_func1 in class A/n";  
    }  
    /**  So it is written in zend studio 8 The complains */ 
    //abstract protected function abstract_func2();  
}  
class C extends B  
{  
    public function abstract_func2()  
    {  
       echo "implement the abstract_func2 in class A/n";  
    }  
}  

4. If we create a subclass B that inherits from A as follows, but do not implement the abstract method abstract_func() : B (); abstract_func(); abstract_func(); abstract_func();


Class B extends A{}; 

The program will have the following error:


 Fatal error: Class B contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (A::abstract_func)
 

5. If B implements the abstract method abstract_func(), then the access control of the ES38en_func () method in B cannot be more stringent than the access control of ES41en_func () in A, i.e. :

(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 the declaration of abstract_func() in A is protected, then the declaration of abstract_func() in B can be public or protected, but cannot be private

(3) If A is declared as private, it cannot be defined as private. (Fatal error: Abstract function A::abstract_func() cannot be declared private)

2. Interface interface

1. An abstract class provides a standard for a concrete implementation, while an interface is a pure template. An interface defines only the functionality, not the content of the implementation. The interface is declared with the keyword interface.

2. interface is completely abstract and can only declare methods, and only methods of public, private and protected, method bodies cannot be defined, and instance variables cannot be declared. However, interface can declare constant variables. But putting a constant variable in interface defeats its purpose as an interface and confuses the different values of interface and classes. If you do need it, you can put it in the corresponding abstract class or Class.


interface iA  
{  
    const AVAR=3;  
    public function iAfunc1();  
    public function iAfunc2();  
}  
echo iA:: AVAR; 

3. Any class that implements an interface implements all the methods defined in the interface


class E implements iA  
{  
    public function iAfunc1(){echo "in iAfunc1";}  
    public function iAfunc2(){echo "in iAfunc2";}  
}  

Otherwise the class must be declared as abstract.


abstract class E implements iA{}  

4. A class can implement an interface in a declaration using the implements keyword. When you do this, the actual process of implementing an interface is no different than inheriting an abstract class that contains only abstract methods. A class can simultaneously inherit from a parent class and implement any number of interfaces. The extends clause should precede the implements clause. PHP only supports inheriting from one parent class, so the extends keyword can only be followed by one class name.


interface iB  
{  
    public function iBfunc1();  
    public function iBfunc2();  
}  
class D extends A implements iA,iB  
{  
    public function abstract_func1()  
    {  
       echo "implement the abstract_func1 in class A/n";  
    }  
    public function abstract_func2()  
    {  
       echo "implement the abstract_func2 in class A/n";  
    }  
    public function iAfunc1(){echo "in iAfunc1";}  
    public function iAfunc2(){echo "in iAfunc2";}  
    public function iBfunc1(){echo "in iBfunc1";}  
    public function iBfunc2(){echo "in iBfunc2";}  
}  

class D extends B implements iA,iB  
{  
    public function abstract_func1()  
    {  
       parent::abstract_func1();  
       echo "override the abstract_func1 in class A/n";  
    }  
    public function abstract_func2()  
    {  
       echo "implement the abstract_func2 in class A/n";  
    }  
    public function iAfunc1(){echo "in iAfunc1";}  
    public function iAfunc2(){echo "in iAfunc2";}  
    public function iBfunc1(){echo "in iBfunc1";}  
    public function iBfunc2(){echo "in iBfunc2";}  
}  

5. An interface may not implement another interface, but may inherit from more than one


interface iC extends iA,iB{}  
class F implements iC  
{  
    public function iAfunc1(){echo "in iAfunc1";}  
    public function iAfunc2(){echo "in iAfunc2";}  
    public function iBfunc1(){echo "in iBfunc1";}  
    public function iBfunc2(){echo "in iBfunc2";}  
}  

3. Similarities and differences between abstract classes and interfaces

1. Similarities:

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

(2) Both interface implementation classes and abstract subclasses must implement declared abstract methods.

2. Differences:

(1) interface need to implement for the implements, while abstract class need inheritance, extends.

(2) A class can implement more than one interface, but a class can only inherit from one abstract.

(3) interface emphasizes the implementation of specific functions, while abstract and class emphasize ownership.

(4) Although interface implementation classes and abstract subclasses must implement the corresponding abstract methods, but the form of implementation is different. Each method in interface is abstract and only declared (declaration has no method body), and the implementation class must implement it. Subclasses of abstract and class can be selectively implemented. This choice has two implications: not all methods in a are abstract, only those with abstract are abstract, and subclasses must be implemented. For methods that do not have abstract, the method body must be defined in abstract. b) subclasses of abstract (abstract) can inherit or override non-abstract methods. For an abstract method, you can choose to implement it or leave it to subclasses to implement it, but this class must also be declared as an abstract class. An abstract class, of course, cannot be instantiated.

(5) abstract class is the intermediary between interface and class. abstract class serves as a link between the preceding and the following in interface and class. 1. abstract class is abstract and can declare abstract methods to specify what subclasses must do. On the other hand, it can define a default method body that can be directly used or overridden by subclasses. In addition, it can define its own instance variables to be used by subclasses through inheritance.

(6) The keyword abstract is not used before abstract methods in the interface. The default implicit method is abstract methods, and the keyword final cannot be added to prevent inheritance of abstract methods. An abstract method in an abstract class must be preceded by abstract to indicate that the display declaration is abstract.

(7) Abstract methods in the interface are public by default, and can only be public, and cannot be modified by private. An abstract method in an abstract class can be modified with public, protected, but not private.

3. Application of interface

(1) Specific interfaces are required to coordinate between classes, regardless of how they are implemented.

(2) It exists as an identifier that can achieve a specific function, or it can be a pure identifier that does not have any interface methods.

(3) A set of classes needs to be treated as singleton classes, and callers can only relate to this set of classes through interfaces.

(4) There are specific features that need to be implemented that may not have any connection at all.

4. abstract class applications

If you need both a unified interface and an instance variable or default method, you can use it. The most common ones are:

(1) Defines a set of interfaces, but does not want to force each implementation class to implement all interfaces. abstract class can be used to define a set of method bodies, or even empty method bodies, and subclasses can choose the methods they are interested in overriding them.

(2) In some cases, the pure interface alone cannot satisfy the coordination between classes, and the variables representing the state in the class are needed to distinguish the different relations. The mediating effect of abstract can satisfactorily satisfy this 1 point.

(3) It standardizes a group of mutually coordinated methods, among which some methods are common, independent of state, and can be shared without the need for subclasses to be implemented separately; Other methods require each subclass to implement a specific function based on its own specific state.


Related articles: