This article mainly introduces the method of PHP interface multi-inheritance and tarits to achieve multi-inheritance effect. It analyzes PHP interface-based multi-inheritance and the simple operation skills of tarits introduced in PHP5.4 to realize multi-inheritance function in the form of examples. It needs Friends can refer to the following
The example of this article describes the method of multiple inheritance of PHP interface and tarits to achieve the effect of multiple inheritance. Share it with everyone for your reference, the details are as follows:
Multiple inheritance of interfaces
In PHP's object-oriented interface, interfaces can inherit interfaces. PHP classes can only inherit from one parent class (single inheritance), but interfaces can implement multiple inheritance and can inherit one or more interfaces. Of course, interface inheritance uses the extends keyword just like class inheritance. If you want multiple inheritances, just separate the inherited interfaces with commas.
It should be noted that when your interface inherits other interfaces, it directly inherits the static constant attributes and abstract methods of the parent interface, so the class must implement all relevant abstract methods when implementing the interface.
The following is an example:
1. Inherit a single interface
"; } function dancing($name){ echo $name."正在跳舞!"; } } $demo=new testC(); $demo->echostr(); $demo->dancing("模特"); //运行结果 /** 接口继承,要实现所有相关抽象方法 模特正在跳舞! **/
2. Inherit multiple interfaces
"; } function dancing($name){ echo $name."正在跳舞!"; echo "
"; } function singing($nickname){ echo $nickname."正在唱歌!"; } } $demo=new testD(); $demo->echostr(); $demo->dancing("模特"); $demo->singing("周杰伦"); //运行结果 /** 接口继承,要实现父接口所有相关方法! 模特正在跳舞! 周杰伦正在唱歌! **/
##taritsMultiple inheritance
In multiple inheritance, a class can inherit multiple parent classes at the same time. The function of combining multiple parent classes in C uses this model to enhance the flexibility of integration. However, multiple inheritance is too flexible and will lead to "diamond inheritance". Therefore, there are many difficulties in using it, and the model has become more complicated. Now most languages have abandoned the multiple inheritance model.But sometimes I want to use multiple inheritance, but PHP doesn’t have multiple inheritance, so I invented such a thing.
traitMethod1 (); $obj-> traitMethod2 (); >
thinkPHP5 framework database coherent operation cache() usage detailed explanation
thinkPHP3.2 Explanation of the method of implementing paging custom styles
Example analysis of multi-image upload function implemented by Laravel framework Blob
The above is the detailed content of PHP interface multiple inheritance and tarits method to achieve multiple inheritance effect_php skills. For more information, please follow other related articles on the PHP Chinese website!