Interface in PHP

PHP interface

The PHP class is single inheritance and does not support multiple inheritance. When a class needs the functions of multiple classes, inheritance is powerless. For this reason, PHP introduces class interface technology.

If all methods in an abstract class are abstract methods, and no variables are declared, and all members in the interface have public permissions, then this special abstract class is called an interface.

The interface is defined with the keyword interface, and the keyword implements is used to implement the methods in the interface, and all methods of the interface must be implemented.

<?php
//定义接口
interface User{
    
    
    function getDiscount();
    function getUserType();
}
//VIP用户 接口实现
class VipUser implements User{
    
    
    // VIP 用户折扣系数
    private $discount = 0.8;
    function getDiscount() {
    
    
        return $this->discount;
    }
    function getUserType() {
    
    
        return "VIP用户";
    }
}
class Goods{
    
    
    var $price = 100;
    var $vc;
    //定义 User 接口类型参数,这时并不知道是什么用户
    function run(User $vc){
    
    
        $this->vc = $vc;
        $discount = $this->vc->getDiscount();
		$usertype = $this->vc->getUserType();
        echo $usertype."商品价格:".$this->price*$discount;
    }
}
$display = new Goods();
$display ->run(new VipUser);
//可以是更多其他用户类型
?>

Run the example, output:

VIP user commodity price: 80 yuan

Implement multiple interfaces

PHP can also implement multiple interfaces at the same time when inheriting a class:

class 子类 extends 父类 implemtns 接口1, 接口2, ...
{
    
    
    ......
}

The difference between abstract class and interface

An interface is a special abstract class (abstract), which can also be regarded as a model specification. The roughly difference between an interface and an abstract class is as follows:

If a subclass implements an interface, it must implement all the methods in the interface (regardless of whether it is needed or not) ; if it inherits an abstract class, it only needs to implement the required methods.
If the method name defined in an interface is changed, then all subclasses that implement this interface need to update the method name synchronously; and if the method name in an abstract class is changed, the corresponding method name of the subclass will not be affected, just become It's just a new method (implementation of the relatively old method).
Abstract classes can only be inherited individually. When the functions that a subclass needs to implement need to be inherited from multiple parent classes, interfaces must be used .

the reason

Developers use interfaces to describe the common behavior of a class or a group of classes.
Why is it only responsible for encapsulating the implementation and not dealing with the detailed information of each class?
This is for decoupling, the interface allows you to change the implementation without changing the details, which is how you use this implementation.

Symfony example

For Symfony, if you want to implement any caching system, the best practice is to do it in the following way

use Symfony\Contracts\Cache\CacheInterface;

class MyClass {
    
    
    private $cache;
    public function __construct(CacheInterface $cache)
    {
    
    
        $this->cache = $cache;
    }
}

Through dependency injection, the cache interface is injected into our class. Next time we modify the caching system, the MyClass class does not need to be changed.

to sum up

The PHP interface is the method template of the class, which is very helpful for decoupling implementation and use.
This feature is especially useful when you need to remain flexible and ensure that all developers follow a set of rules.

expand

In addition to interface and static classes, we can also use: Trait

Traits is a code reuse mechanism for single inheritance languages ​​like PHP. Trait in order to reduce the limitations of single-inheritance languages, allowing developers to freely reuse methods in independent classes in different hierarchical structures. The semantics of the combination of Trait and Class defines a way to reduce complexity and avoid traditional multiple inheritance problems.

  • The usage of Trait is very simple and you can use it directly in the class.

  • Note: The method in Trait will override the method of the same name in the base class, and this class will override the method of the same name in Trait . When the trait defines the attribute, the class cannot define the attribute with the same name, otherwise a fatal error will occur, unless it is set to the same visibility and the same default value.

Reprinted from: http://www.5idev.com/p-php_class_interface_implements.shtml

Guess you like

Origin blog.csdn.net/qq_39004843/article/details/108165451