小编Hac*_*Kan的帖子

PHP:使用相同名称的方法处理特征

我已经在这个网站和Google上进行了搜索,最终创建了这个帐户...
我需要有关php,特征和类的帮助。我有这2个不同的特征,其中一些方法具有相同的名称。

问题取决于我需要他们两个!我不能代替...

这是一个示例代码:http : //sandbox.onlinephpfunctions.com/code/b69f8e73cccd2dfd16f9d24c5d8502b21083c1a3

trait traitA {
    protected $myvar;
    public function myfunc($a) { $this->myvar = $a; }
}

trait traitB
{
    public function myfunc($a) { $this->myvar = $a * $a; }
}


class myclass 
{
    protected $othervar;

    use traitA, traitB {
        traitA::myfunc as protected t1_myfunc;
        traitB::myfunc as protected t2_myfunc;
    }

    public function func($a) {
        $this->myvar = $a * 10;
        $this->othervar = t2_myfunc($a);
    }

    public function get() { 
        echo "myvar: " . $this->myvar . " - othervar: " …
Run Code Online (Sandbox Code Playgroud)

php inheritance scope class traits

7
推荐指数
1
解决办法
2840
查看次数

标签 统计

class ×1

inheritance ×1

php ×1

scope ×1

traits ×1