1

我目前正在这样做,但似乎不是正确的方法:

class Name
{
    protected $jobs2do;

    public function __construct($string) {
        $this->jobs2do[] = $this->do;
    }

        public function do() {
        ...
    }
}

因为直接分配一个函数会引起警告,应该这样做:

function func()
{
...
}

$func_array[] = 'func';

比如说,把它变成一个字符串,但是当它是一个成员函数时我不知道该怎么做。

以下版本可以吗?:

class Name
{
    public $jobs2do;

    public function __construct($string) {
        $this->jobs2do[] = array($this,'do');
    }

        public function do() {
        ...
    }
}

调用它时,只需:

$instance = new Name();
foreach ($instance->jobs2do as $job)
{
    call_user_func($job);
}
4

6 回答 6

1

(至少)有 4 种方法可以做到这一点。还有其他方法,但这些是最纯粹的。方法版本至少需要 PHP5。

class foo {
    public function bar($a) { return $a; }
}

$anObject = new foo();
$ret = call_user_func(array($anObject,'bar'), 1);
$ret = call_user_func_array(array($anObject,'bar'), array(1));
$ret = call_user_method('bar', $anObject, array(1));
$ret = call_user_method_array('bar', $anObjectm, 1);

您也可以用字符串变量替换“bar”。

于 2009-06-04T13:53:10.213 回答
0

看一下可调用的伪类型。然后,您可以使用call_user_func()调用该函数:

class Foo {
    function bar($str) {
        echo($str);
    }
}

$foo = new Foo();
$func_array = array();
$func_array['foo->bar'] = array($foo, 'bar');  // this is the 'callable' pseudo-type

call_user_func($func_array['foo->bar'], "Hello World");



编辑:看来您正在尝试实现命令模式。在这种情况下,您可以尝试以下方式:

// define an interface for all actions
interface Executable {
    public function do();
}

// an example action
class DoSomething implements Executable {
    private $name;

    public __construct($name) {
        $this->name = $name;
    }

    public function do($str) {
        echo("Hello $str, my name is $this->name");
    }
}

$objects_to_process = array(new DoSomething("Foo"), new DoSomething("Bar"));
foreach ($objects_to_process as $obj) {
    if ($obj instanceof Executable)
        $obj->do("World");
}
于 2009-06-04T10:14:15.867 回答
0

请检查call_user_func()call_user_func_array()

于 2009-06-04T10:15:47.110 回答
0

为 Job 编写一个类并使用工厂模式来创建它们。然后写一个类 JobManager 应该维护一个列表 ob Job 实例。

interface iCallableJob
{
    public function run();
}

class Job implements iCallableJob
{
    // The parameterized factory method
    public static function factory($type)
    {
        $classname = 'Job_' . $type;
        if (class_exists($classname) && in_array('iCallableJob', class_implements('Job')))
        {
            return new $classname();
        }
        return null;
    }
    public function run() { return null; }
}

class Job_type1 extends Job 
{
    public function run() 
    {
        echo 'Job 1';
    }
}

class JobManager
{
    protected $jobs2do = array();

    public function addJob($type)
    {
        if ($job = Job::factory($type))
        {
            $this->jobs2do[] = $job;
            return $job;
        }
        return null;
    }
    public function runAll()
    {
        foreach ($this->jobs2do AS $job)
        {
            $job->run();
        }
    }
}
于 2009-06-04T10:35:42.170 回答
0

这个问题可以有几种可能的解决方案。您在其他回复中显示了命令和工厂模式。这一个说明了如何使用访问者+命令模式合作。

class Name {

    protected $jobs = array();

    public function addJob(IJob $j) {
        $this->jobs[] = $j;
    }

    public function do() {
        foreach ($this->jobs as $j) {
            $j->run($this);
        }
    }

}

interface IJob {

    public function run(Name $invoker);

}

class WashDishes implements IJob {

    public function run(Name $invoker) {
        echo get_class($invoker) . ' washes dishes<br/>';
    }

}

class DoShopping implements IJob {

    public function run(Name $invoker) {
        echo get_class($invoker) . ' does shopping<br/>';
    }

}

$n = new Name();
$n->addJob(new WashDishes());
$n->addJob(new DoShopping());
$n->do();

输出:

Name washes dishes
Name does shopping

实现IJob接口的类是可以传递和存储以供以后执行的命令。Name对象在集合中调用作业(传递引用)的方式$this是访问者模式的典型方式(这种方式作业对象可以访问其调用者 -Name对象)。但是,由于缺乏对显式方法覆盖的本地支持,真正的访问者模式在 PHP 中是不可能的。

于 2009-06-04T13:44:00.513 回答
0

不知道这是否是正确的方法。但这就是我的做法。

$this->api['google']['+1Button']=function($str)
    {
        echo $str;
    };
    $this->api['google']['+1Button']("hello world");
于 2013-07-30T20:42:41.750 回答