0

Which is better and why:

call_user_func(array($class, $method), $params);

or

$class::$method($params);

the second approach only works in PHP 5.3. I in older virsion in throws a fatal error unexpected T_PAAMAYIM_NEKUDOTAYIM. (my ide thinks its a syntax error, too. But it works)

3
  • 1
    My understanding is that both approaches will result in the same effective runtime approach to calling the function, and should perform about the same. Commented Dec 9, 2010 at 19:55
  • As soon as your question contains the words "which is better," you should look to programmers.stackexchange.com for answers. Commented Dec 9, 2010 at 19:56
  • Also, the second only can do methods that take params as an array of items. The first can access methods that take any kind of parameters. Which should be easier to read in the class. Commented Dec 9, 2010 at 20:09

3 Answers 3

3

Well if portability is important for you, then the obvious answer that you should use the first way. If you know that you're always targeting at least PHP 5.3, then you should use the method that you feel most comfortable yourself. The $class::$method($params); is shorter and cleaner, so I'd go for it myself.

Sign up to request clarification or add additional context in comments.

Comments

1

Both are just fine. It's my opinion that the second is more syntactically clear, though. As you stated, the only downside is that it requires 5.3+.

Comments

0

With PHP 5.3.0, there is also the forward_static_call() function, although there's slightly more restrictions to its use

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.