NYCPHP Meetup

[nycphp-talk] function references

Elijah Insua tmpvar at gmail.com
Sat Jan 10 17:58:50 EST 2009


umm... with objects this gets sexy.


class Add {
    public function execute($a, $b) { return $a + $b; }
}

class Subtract {
    public function execute($a, $b) { return $a - $b; }
}

$executors = array(array('object'=>new Add(), 'params' => array(1,1)));

foreach ($executors as $execute)
{
    echo "Result: " . call_user_funct_array(array($execute['object'],
'execute'), $execute['params']));
}



On Sat, Jan 10, 2009 at 5:43 PM, Brian O'Connor <gatzby3jr at gmail.com> wrote:

> Oh sorry, forgot to add this too, which I think better fits your question:
>
> <?php
>
> function my_func($a) {
>     print $a;
> }
>
> $a = 'my_func';
>
> $a('hello');
>
> ?>
>
> prints:
>
> hello
>
>
> On Sat, Jan 10, 2009 at 5:40 PM, Brian O'Connor <gatzby3jr at gmail.com>wrote:
>
>> I believe this is what you're looking for.
>>
>> I've done similar things in the past, but I don't have the code handy.
>>
>> http://www.php.net/manual/en/language.variables.variable.php
>>
>> On Sat, Jan 10, 2009 at 2:45 PM, Jesse Callaway <bonsaime at gmail.com>wrote:
>>
>>> Is it possible to take a reference to a function and then stick it in an
>>> array?
>>>
>>> I'd like to have an array with function names as the keys and the values
>>> would be references to the actual function. This way I could do
>>>
>>>
>>> do[$that_function];
>>>
>>> Basically being lazy and don't want to have a big ugly switch block when
>>> I could just declare the array, the functions, and then call it all in one
>>> line.
>>>
>>> Not even sure what the calling syntax would be, so I'm not sticking to
>>> this example but it shows the idea I hope.
>>>
>>>
>>> Is this more commonly done with some sort of eval() function?
>>>
>>> -jesse
>>>
>>> _______________________________________________
>>> New York PHP User Group Community Talk Mailing List
>>> http://lists.nyphp.org/mailman/listinfo/talk
>>>
>>> http://www.nyphp.org/show_participation.php
>>>
>>
>>
>>
>> --
>> Brian O'Connor
>>
>
>
>
> --
> Brian O'Connor
>
> _______________________________________________
> New York PHP User Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> http://www.nyphp.org/show_participation.php
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20090110/b17b2612/attachment.html>


More information about the talk mailing list