Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-伪造多重继承-如何将多个参数传递给函数_Php_Parameter Passing_Multiple Inheritance - Fatal编程技术网

PHP-伪造多重继承-如何将多个参数传递给函数

PHP-伪造多重继承-如何将多个参数传递给函数,php,parameter-passing,multiple-inheritance,Php,Parameter Passing,Multiple Inheritance,我在搜索如何在PHP中伪造多重继承时发现了这一点(因为PHP不直接支持多重继承) 以下是此处给出的完整代码:- class B { public function method_from_b($s) { echo $s; } } class C { public function method_from_c($s) { echo $s; } } class A extends B { private $c; publ

我在搜索如何在PHP中伪造多重继承时发现了这一点(因为PHP不直接支持多重继承)

以下是此处给出的完整代码:-

class B {
    public function method_from_b($s) {
        echo $s;
    }
}

class C {
    public function method_from_c($s) {
        echo $s;
    }
}

class A extends B
{
  private $c;

  public function __construct()
  {
    $this->c = new C;
  }

  // fake "extends C" using magic function
  public function __call($method, $args)
  {
    $this->c->$method($args[0]);
  }
}


$a = new A;
$a->method_from_b("abc");
$a->method_from_c("def");
问题
这里给出的示例只考虑函数
C::method\u from_C($s)
的一个参数。它只使用一个参数就可以正常工作,但我有几个
类C
的函数,有些函数有2个,有些函数有3个参数,如下所示:-

class C {
    public function method_from_c($one,$two) {
        return $someValue;
    }

    public function another_method_from_c($one,$two, $three) {
        return $someValue;
    }
}
public function method_from_c() 
{

     $args = func_get_args();

     //extract params from $args and then treat each parameter
}
我不想改变C类函数定义中的任何内容(它必须接受这些参数)。例如,我不想在我的
C::method\u from\u C($s$two)
中使用func\u get\u args(),如下所示:-

class C {
    public function method_from_c($one,$two) {
        return $someValue;
    }

    public function another_method_from_c($one,$two, $three) {
        return $someValue;
    }
}
public function method_from_c() 
{

     $args = func_get_args();

     //extract params from $args and then treat each parameter
}
A类
\u call()
函数内部执行什么操作,以使其正常工作。我希望能够从C($1,$2)调用
类C
函数,如
$obj->method\u

谢谢
Sandepan

您可以使用:

请注意,这不会很好地执行。

您可以使用:


请注意,这样做的效果并不好。

您可以在


你可以在这里找到答案


Raoul Duke的答案比我的好-(call\u user\u方法fns不推荐使用)Raoul Duke的答案比我的好-(call\u user\u方法fns不推荐使用)$this->c的引用运算符是redundant@sandeepan:事实上我不确定,但我模糊地记得在某处读到过$foo->$bar()性能优于使用call\u user\u func。$this->c的引用运算符为redundant@sandeepan:实际上我不确定,但我隐约记得在某个地方读到过,$foo->$bar()比使用call\u user\u func性能更好。