Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The manual uses the terms "callback" and "callable" interchangeably, however, "callback" traditionally refers to a string or array value that acts like a <a href="http://en.wikipedia.org/wiki/Function_pointer" rel="noreferrer">function pointer</a>, referencing a function or class method for future invocation. This has allowed some elements of functional programming since PHP 4. The flavors are:</p> <pre><code>$cb1 = 'someGlobalFunction'; $cb2 = ['ClassName', 'someStaticMethod']; $cb3 = [$object, 'somePublicMethod']; // this syntax is callable since PHP 5.2.3 but a string containing it // cannot be called directly $cb2 = 'ClassName::someStaticMethod'; $cb2(); // fatal error // legacy syntax for PHP 4 $cb3 = array(&amp;$object, 'somePublicMethod'); </code></pre> <p>This is a safe way to use callable values in general:</p> <pre><code>if (is_callable($cb2)) { // Autoloading will be invoked to load the class "ClassName" if it's not // yet defined, and PHP will check that the class has a method // "someStaticMethod". Note that is_callable() will NOT verify that the // method can safely be executed in static context. $returnValue = call_user_func($cb2, $arg1, $arg2); } </code></pre> <p>Modern PHP versions allow the first three formats above to be invoked directly as <code>$cb()</code>. <code>call_user_func</code> and <code>call_user_func_array</code> support all the above.</p> <p>See: <a href="http://php.net/manual/en/language.types.callable.php" rel="noreferrer">http://php.net/manual/en/language.types.callable.php</a></p> <p>Notes/Caveats: </p> <ol> <li>If the function/class is namespaced, the string must contain the fully-qualified name. E.g. <code>['Vendor\Package\Foo', 'method']</code></li> <li><code>call_user_func</code> does not support passing non-objects by reference, so you can either use <code>call_user_func_array</code> or, in later PHP versions, save the callback to a var and use the direct syntax: <code>$cb()</code>;</li> <li>Objects with an <a href="http://us2.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.invoke" rel="noreferrer"><code>__invoke()</code></a> method (including anonymous functions) fall under the category "callable" and can be used the same way, but I personally don't associate these with the legacy "callback" term.</li> <li>The legacy <code>create_function()</code> creates a global function and returns its name. It's a wrapper for <code>eval()</code> and anonymous functions should be used instead.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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