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.
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...
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.
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?
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.
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.