PHP中代码管道的概念

PHP中代码管道的概念,php,Php,假设您想从另一个对象调用几个方法。正确的做法是什么 如果使用\u call(),是否可以提取参数,而不是将其用作数组 例如: <?php class Component { protected $borrowMethods = array(); public function __call( $name, $args ) { if( isset( $this->borrowMethods[$name] ) ) {

假设您想从另一个对象调用几个方法。正确的做法是什么

如果使用
\u call()
,是否可以提取参数,而不是将其用作数组

例如:

<?php

class Component
{
    protected $borrowMethods = array();

    public function __call( $name, $args )
    {
        if( isset( $this->borrowMethods[$name] ) )
        {
            $obj = $this->borrowMethods[$name] ;
            return $obj->$name( $this->argExtractFunc($args) );
        }

        throw new \Exception( 'method not exists' );
    }
}

class ActiveRecord extends Component
{
    protected $validator; //instance of validator 

    protected $borrowMethods = array(

        'validate' => 'validator',
        'getError' => 'validator',
        'moreMethods' => 'someOtherClass',
    );

    public function save()
    {
        if($this->validate())
        {

        }
    }
}

class Validator
{

    public function validate(){}

    public function getError( $field ){}

}

$ar = new ActiveRecord;

$ar->getError( $field );

我不确定我完全不理解你的要求,但我相信你所指的是众所周知的。您的每个方法都需要返回
$this
(或另一个对象),然后原始调用方可以立即对其调用另一个方法

class Test
{
    public function one() {
        echo 'one';
        return $this;
    }

    public function two() {
        echo 'two';
        return $this;
    }

}

$test = new Test();
$test->one()->two();  // <-- This is what I think you're trying to do
类测试
{
公共职能一{
呼应"一",;
退还$this;
}
公共职能二{
呼应"二",;
退还$this;
}
}
$test=新测试();

$test->one()->two();//getError($field)

您要查找的是方法链接。请参见本主题:


\uuu call()
中的参数可以通过以下方式获得:
func\u get\u args()

您可以将您试图执行的操作放入伪代码吗?在
\uu call
中,第二个参数包含参数数组。“提取论点”是什么意思?为什么你不能使用数组?我知道链接,你在寻找
returncall\u user\u func\u数组(数组($obj,$name),$args)?是的,对,危险品,这就是我要找的。我不知道我怎么会错过它。谢谢你,伙计。