Implementing callbacks -- which do you prefer

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Implementing callbacks -- which do you prefer

Post by alex.barylski »

Seeing as this is for an (eventual) open source project...I'd like personal opinions or constructive criticism why one method is prefered over the other...

Example 1:

Code: Select all

call_user_func_array('my_method', $args)
Example 2:

Code: Select all

 
$func = 'my_method';
$func($args);
Personally I don't mind either and maybe even prefer Example 2 BUT...I can see the code being confusing to someone just visiting the first time especially if they are not overly familiar with PHP and it's dynamic nature...whereas if I used a call_user_* approach...when they seen that code they would at least have the PHP docs to lookup and quickly determine what it was the code was doing.

Also I would think it might have the additional advantage of increased performance as the additional tokenizing/parsing would likely be required to support the more runtime dynamic approach of Example 2...but who knows I'm not about to profile for something so trivial.

What do you prefer?
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Implementing callbacks -- which do you prefer

Post by ghurtado »

I think I would choose the first example (it is more explicit) if I had no choice but to dynamically determine what method to call. Now, I know that there are a few edge cases where it is your best option (like dynamic proxies or front controllers), but 90% of the time I have seen the idiom used, it was unnecessary. Kindof like eval.

Out of curiosity, what is the context of the code?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Implementing callbacks -- which do you prefer

Post by alex.barylski »

Thanks for the feedback...the more I think about it the more I want to lean towards one too...

The purpose is a module loader for a CMS.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Implementing callbacks -- which do you prefer

Post by onion2k »

The first way is the only one that lets you call a static method from a class isn't it? I like bundling things up in classes so I tend to use that. Not that I do it very often.
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Implementing callbacks -- which do you prefer

Post by ghurtado »

onion2k wrote:The first way is the only one that lets you call a static method from a class isn't it? I like bundling things up in classes so I tend to use that. Not that I do it very often.
Good catch. Not only that, but it is also the only way to call a function or method when the number of arguments is only known at runtime. In this context, $func($args); is unlikely to do what you would expect it to do, since it will pass $args as the first argument to the function.
Post Reply