Re: $arr = array('Hello', 'world'); $arr();
| From: | Etienne Kneuss | Date: | Sun, 05 Jun 2011 16:41:35 +0000 |
| Subject: | Re: $arr = array('Hello', 'world'); $arr(); | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-52945@lists.php.net to get a copy of this message | ||
I consider this an improvement in terms of consistency w.r.t.
callbacks, so +1 from me, good job!
Best,
On Sun, Jun 5, 2011 at 18:21, Felipe Pena <felipensp@gmail.com> wrote:
> 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
>
--
Etienne Kneuss
http://www.colder.ch