Re: $arr = array('Hello', 'world'); $arr();
| From: | Felipe Pena | Date: | Sun, 05 Jun 2011 16:21:19 +0000 |
| Subject: | Re: $arr = array('Hello', 'world'); $arr(); | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-52942@lists.php.net to get a copy of this message | ||
2011/6/5 Benjamin Eberlei <kontakt@beberlei.de>
> That can lead to quite a bit of simplifications in code where you now have
> to check for is_array/is_callable/instanceof Closure and such. I like it.
>
>
Exactly, and since our current $x = 'hello::world'; $x(); doesn't support
method calls, the array one can help on this just like the call_user_func()
approach with arrays.
<?php
class Hello {
static public function world($x) {
echo "Hello, $x\n";
}
}
function hello_world($x) {
echo "Hello, $x\n";
}
$callbacks = array(
array('Hello', 'world'),
function ($x) { echo "Hello, $x\n"; },
'hello_world'
);
foreach ($callbacks as $k => $callback) {
if (is_callable($callback)) {
$callback($k);
}
}
Output:
Hello, 0
Hello, 1
Hello, 2
--
Regards,
Felipe Pena