Home  >  Article  >  Backend Development  >  How to use trait or combination mode to implement multiple inheritance in PHP

How to use trait or combination mode to implement multiple inheritance in PHP

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-07-21 16:17:542299browse

PHP’s class inheritance can only be single inheritance, not multiple inheritance. If you want to achieve multiple inheritance, you can use the interface method to achieve multiple inheritance. However, if you don’t want to use the interface method to achieve multiple inheritance, you can consider using Traits are implemented, and of course, they can also be implemented using the composition pattern.

How to use trait or combination mode to implement multiple inheritance in PHP

Trait is a new code reuse method in PHP 5.4. Trait is a code reuse mechanism prepared for single inheritance languages ​​like PHP. Traits are designed to reduce the limitations of single-inheritance languages ​​and allow developers to freely reuse methods in independent classes within different hierarchies. The semantics of Trait and Class composition define a way to reduce complexity and avoid the typical problems associated with traditional multiple inheritance and Mixin classes.

Trait is similar to Class, but is only designed to combine functionality in a fine-grained and consistent way. Cannot be instantiated through the trait itself. It adds a combination of horizontal features to traditional inheritance; that is, there is no need for inheritance between several Classes in an application.

Usually use the use keyword in a class to declare the Trait name to be combined, and the declaration of a specific Trait uses the trait keyword, and the Trait cannot be instantiated directly.

The code is as follows:

<?php
/**
 * Traits可以多重继承,可以看做是一种加强型的接口
 */
trait Hello
{
    public function sayHello()
    {
        echo &#39;Hello &#39;;
    }
    public function aaa()
    {
        echo &#39;AAA&#39;;
    }
}
trait World
{
    public function sayWorld()
    {
        echo &#39;World&#39;;
    }
}
class MyHelloWorld
{
    use Hello, World;
    public function sayExclamationMark()
    {
        echo &#39;!&#39;;
    }
    // 会覆盖掉Hello中的aaa方法
    public function aaa()
    {
        echo &#39;AAAAA&#39;;
    }
}
$obj = new MyHelloWorld();
$obj->sayHello();
$obj->sayWorld();
$obj->sayExclamationMark();
$obj->aaa();
// 输出:
// Hello World!AAAAA

The code uses two traits to implement multiple inheritance.

Note: The same method cannot appear in two traits. If there is a method with the same name, a fatal error will occur.

Note: There is also aaa method in MyHelloWorld class, which will override the aaa method in Hello trait.

The above code can also be implemented through the combined mode. The code is as follows:

<?php
/**
 * 使用组合的方式也可以达到 1_12_trait.php 的效果
 */

class Hello2
{
    public function sayHello()
    {
        echo &#39;Hello &#39;;
    }

    public function aaa()
    {
        echo &#39;AAA&#39;;
    }
}

class World2
{
    public function sayWorld()
    {
        echo &#39;World&#39;;
    }

    public function aaa()
    {
        echo &#39;AAAA&#39;;
    }
}

class MyHelloWorld2
{
    public $obj = [];

    public function __construct()
    {
        $this->obj[&#39;Hello2&#39;] = new Hello2();
        $this->obj[&#39;World2&#39;] = new World2();
    }

    public function sayExclamationMark()
    {
        echo &#39;!&#39;;
    }

    public function __call($name, $arguments)
    {
        foreach ($this->obj as $value) {
            if (method_exists($value, $name)) {
               return $value->$name();
                // 也可以用 return call_user_func([$value, $name]);
            }
        }

        if (!method_exists($this->obj[&#39;Hello2&#39;], $name) && !method_exists($this->obj[&#39;World2&#39;], $name)) {
            echo "\r\n不存在此方法";
        }

        return true;
    }

    // 会覆盖其他类的aaa方法
    public function aaa()
    {
        echo &#39;AAAAA&#39;;
    }
}

$obj = new MyHelloWorld2();
$obj->sayHello();
$obj->sayWorld();
$obj->saywhat();
$obj->aaa();
$obj->sayExclamationMark();

// 输出:
// Hello World!AAAAA

Obviously, if implemented through traits, the amount of code will be much less and the readability will be much higher.

Recommended learning: php video tutorial

The above is the detailed content of How to use trait or combination mode to implement multiple inheritance in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete