Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is at leat two differences between <a href="http://php.net/forward_static_call_array" rel="noreferrer"><code>forward_static_call_array</code></a> and <a href="http://php.net/call_user_func_array" rel="noreferrer"><code>call_user_func_array</code></a> :</p> <ul> <li>The first one only exists since PHP 5.3</li> <li>The first one must be called from inside a class</li> </ul> <p>After that, I suppose there is some difference that's related to Late Static Binding, that was introduced with PHP 5.3.</p> <p><br> Actually, if you take a closer look at the given example, it seems to be exactly that : the "context" of the class inside which you are using <code>forward_static_call_array</code> is "kept", in the called method.</p> <p>Considering this portion of code, that's derived from the given example :</p> <pre><code>class A { const NAME = 'A'; public static function test() { $args = func_get_args(); echo static::NAME, " ".join(',', $args)." \n"; // Will echo B } } class B extends A { const NAME = 'B'; public static function test() { echo self::NAME, "\n"; // B forward_static_call_array(array('A', 'test'), array('more', 'args')); } } B::test('foo'); </code></pre> <p>You'll get this output :</p> <pre><code>B B more,args </code></pre> <p>i.e. from the method in class A, you "know", via the <code>static::</code> keyword, that you're "coming from B".</p> <p><br> Now, if you try to do the the same thing with <code>call_user_func</code> :</p> <pre><code>class B extends A { const NAME = 'B'; public static function test() { echo self::NAME, "\n"; // B call_user_func_array(array('A', 'test'), array('more', 'args')); } } </code></pre> <p><em>(the rest of the code doesn't change)</em></p> <p>You'll get this output :</p> <pre><code>B A more,args </code></pre> <p>Note the <strong>A</strong> on the second line ! With <code>forward_static_call_array</code>, you didn't get an <strong>A</strong>, but a <strong>B</strong>.</p> <p>That's the difference : <code>forward_static_call_array</code> forwards the static context to the method that's called, while <code>call_user_func_array</code> doesn't.</p> <p><br> About your efficiency question : I have no idea -- you'd have to benchmark ; but that's really not the point : the point is that those two functions don't do the same thing.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload