当前位置 : 主页 > 网络编程 > 其它编程 >

如何将成员函数分配给数组并调用它-Howtoassignamemberfunctiontoanarrayandcallit

来源:互联网 收集:自由互联 发布时间:2023-07-02
Imcurrentlydoingitthisway,butseemstonotbetheproperway:我目前正在这样做,但似乎不是正确的方式:classNam I'm currently doing it this way, but seems to not be the proper way: 我目前正在这样做,但似乎不是正确的方式
Imcurrentlydoingitthisway,butseemstonotbetheproperway:我目前正在这样做,但似乎不是正确的方式:classNam

I'm currently doing it this way, but seems to not be the proper way:

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

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

Because directly assign a function will cause warning, should do something like:

因为直接分配一个函数会引起警告,应该做类似的事情:

function func(){...}$func_array[] = 'func';

say, put it into a string, but I don't know how to do it when it's a member function.

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

Is the following version OK?:

以下版本可以吗?:

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

when call it, just:

在打电话时,只需:

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

6 个解决方案

#1

Write a class for Job and use factory pattern to create them. Then Write a class JobManager wihich should maintain a list ob Job instances.

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

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

#2

There are (at least) 4 ways to do this. There are other ways but these are the most pure. The method versions require PHP5 at a minimum.

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

You can replace 'bar' with a string variable, too.

你也可以用字符串变量替换'bar'。

#3

Please check call_user_func() or call_user_func_array()

请检查call_user_func()或call_user_func_array()

#4

Take a look at the callable pseudo type. You can then call that function with call_user_func():

看看可调用的伪类型。然后,您可以使用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-typecall_user_func($func_array['foo->bar'], "Hello World");

Edit: It seems you're trying to implement the command pattern. In that case you could try something along these lines:

编辑:您似乎正在尝试实现命令模式。在这种情况下,你可以尝试这些方面的东西:

// define an interface for all actionsinterface Executable { public function do();}// an example actionclass 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");}

#5

There can be several possible solutions to this problem. You have Command and Factory patterns presented in other replies. This one illustrates how to use Visitor + Command pattern cooperation.

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

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'; }}class DoShopping implements IJob { public function run(Name $invoker) { echo get_class($invoker) . ' does shopping'; }}$n = new Name();$n->addJob(new WashDishes());$n->addJob(new DoShopping());$n->do();

Output:

Name washes dishesName does shopping

A class implementing IJob interface is a command that can be passed and stored for later execution. The way Name object invokes jobs in collection (passing $this reference) is typical for Visitor pattern (this way job object has access its caller - Name object). However, true Visitor pattern is not possible in PHP due to lack of native support for explicit method overriding.

实现IJob接口的类是一个可以传递和存储以供以后执行的命令。 Name对象调用集合中的作业(传递$ this引用)的方式对于Visitor模式是典型的(这样作业对象可以访问其调用者 - Name对象)。但是,由于缺少对显式方法重写的本机支持,因此PHP中无法实现真正​​的访问者模式。

#6

dont know if this is the correct way. but this is how i do mine.

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

$this->api['google']['+1Button']=function($str) { echo $str; }; $this->api['google']['+1Button']("hello world");【本文转自:美国服务器 http://www.558idc.com/mg.html欢迎留下您的宝贵建议
网友评论