将变量定义为函数,作为参数传递,并在该函数中调用它 - PHP

时间:2014-06-12 20:48:50

标签: php delegates

我有以下内容:

class Delegator
{
    public $Create;
    public $Update;
    public $Delete;
}

// Inside controller 1

$delegator = new Delegator();
$delegator->Create = function(){ /**/ };
$delegator->Update = function(){ /**/ };
$delegator->Delete = function(){ /**/ };

InvokeAction( $delegator );

//-------------------

// Inside controller 2

function InvokeAction( Delegator &$delegator )
{
    switch( $something )
    {
        case "create":
            $delegator->Create(); // Says Create method is undefined.
        break;
    }
}

//-------------------

在Invoke动作函数中,它调用Create变量就好像它是一个函数,但它看不到它?我有什么想法?

ANSWER ----------------

call_user_func( $delegator->Create, /* parameters go here if needs be */ );

0 个答案:

没有答案