2015/07/09

How to invoke object method with arguments passed as argument in PHP ?

class Test
{
public function f1(array $keywords)
{
foreach($keywords as $keyword)
{
echo "f1():" . $keyword . "<br/>";
}
}
public function f2(array $keywords)
{
foreach($keywords as $keyword)
{
echo "f2():" . $keyword . "<br/>";
}
}
public function start()
{
$this->run([$this, 'f1'], ['aaa', 'bbb']);
$this->run([$this, 'f2'], ['aaa', 'bbb']);
}
public function run($func, $args)
{
$func($args);
}
}
$test = new Test();
$test->start();
// output
// f1():aaa
// f1():bbb
// f2():aaa
// f2():bbb
view raw gistfile1.php hosted with ❤ by GitHub

No comments:

Post a Comment