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

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);
}
有帮助吗?

解决方案

请为工作类并使用工厂图案创建它们。然后写一个类JobManager wihich应保持列表转播工作情况。

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();
        }
    }
}

其他提示

有(至少)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);

可以用一个字符串变量替换“酒吧”,太

看一看的可调用伪类型。然后可以调用该函数与 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");
}

这个问题可以有多种可能的解决方案。您在其他回复中介绍了命令和工厂模式。这个说明了如何使用 游客 + 命令 模式合作。

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 中是不可能的。

不知道这是否是正确的方法。但是这是怎么做矿。

$this->api['google']['+1Button']=function($str)
    {
        echo $str;
    };
    $this->api['google']['+1Button']("hello world");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top