downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the Git repository for this website on git.php.net.

Source of: /manual/en/language.types.callable.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once
dirname(__FILE__) ."/toc/language.types.inc";
$setup = array (
 
'home' =>
  array (
   
0 => 'index.php',
   
1 => 'PHP Manual',
  ),
 
'head' =>
  array (
   
0 => 'UTF-8',
   
1 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'language.types.callable.php',
   
1 => 'Callbacks',
  ),
 
'up' =>
  array (
   
0 => 'language.types.php',
   
1 => 'Types',
  ),
 
'prev' =>
  array (
   
0 => 'language.types.null.php',
   
1 => 'NULL',
  ),
 
'next' =>
  array (
   
0 => 'language.pseudo-types.php',
   
1 => 'Pseudo-types and variables used in this documentation',
  ),
 
'alternatives' =>
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div id="language.types.callable" class="sect1">
 <h2 class="title">Callbacks</h2>
 
 <p class="para">
  Callbacks can be denoted by <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> type hint as of PHP 5.4.
  This documentation used <span class="type"><a href="language.pseudo-types.php#language.types.callback" class="type callback">callback</a></span> type information for the same
  purpose.
 </p>

 <p class="para">
  Some functions like  <span class="function"><a href="function.call-user-func.php" class="function">call_user_func()</a></span> or
   <span class="function"><a href="function.usort.php" class="function">usort()</a></span> accept user-defined callback functions as a
  parameter. Callback functions can not only be simple functions, but also
  <span class="type"><a href="language.types.object.php" class="type object">object</a></span> methods, including static class methods.
 </p>

 <div class="sect2" id="language.types.callable.passing">
  <h3 class="title">Passing</h3>

  <p class="para">
   A PHP function is passed by its name as a <span class="type"><a href="language.types.string.php" class="type string">string</a></span>. Any built-in
   or user-defined function can be used, except language constructs such as:
    <span class="function"><a href="function.array.php" class="function">array()</a></span>,  <span class="function"><a href="function.echo.php" class="function">echo</a></span>,
    <span class="function"><a href="function.empty.php" class="function">empty()</a></span>,  <span class="function"><a href="function.eval.php" class="function">eval()</a></span>,
    <span class="function"><a href="function.exit.php" class="function">exit()</a></span>,  <span class="function"><a href="function.isset.php" class="function">isset()</a></span>,
    <span class="function"><a href="function.list.php" class="function">list()</a></span>,  <span class="function"><a href="function.print.php" class="function">print</a></span> or
    <span class="function"><a href="function.unset.php" class="function">unset()</a></span>.
  </p>

  <p class="para">
   A method of an instantiated <span class="type"><a href="language.types.object.php" class="type object">object</a></span> is passed as an
   <span class="type"><a href="language.types.array.php" class="type array">array</a></span> containing an <span class="type"><a href="language.types.object.php" class="type object">object</a></span> at index 0 and the
   method name at index 1.
  </p>

  <p class="para">
   Static class methods can also be passed without instantiating an
   <span class="type"><a href="language.types.object.php" class="type object">object</a></span> of that class by passing the class name instead of an
   <span class="type"><a href="language.types.object.php" class="type object">object</a></span> at index 0.
   As of PHP 5.2.3, it is also possible to pass
   <em>&#039;ClassName::methodName&#039;</em>.
  </p>

  <p class="para">
   Apart from common user-defined function,  <span class="function"><a href="function.create-function.php" class="function">create_function()</a></span>
   can also be used to create an anonymous callback function. As of PHP 5.3.0
   it is possible to also pass a
   <a href="functions.anonymous.php" class="link">closure</a> to a callback parameter.
  </p>

  <p class="para">
   <div class="example" id="example-96">
    <p><strong>Example #1
     Callback function examples
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php&nbsp;<br /><br /></span><span style="color: #FF8000">//&nbsp;An&nbsp;example&nbsp;callback&nbsp;function<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">my_callback_function</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'hello&nbsp;world!'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;An&nbsp;example&nbsp;callback&nbsp;method<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyClass&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;function&nbsp;</span><span style="color: #0000BB">myCallbackMethod</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Hello&nbsp;World!'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Type&nbsp;1:&nbsp;Simple&nbsp;callback<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(</span><span style="color: #DD0000">'my_callback_function'</span><span style="color: #007700">);&nbsp;<br /><br /></span><span style="color: #FF8000">//&nbsp;Type&nbsp;2:&nbsp;Static&nbsp;class&nbsp;method&nbsp;call<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'MyClass'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'myCallbackMethod'</span><span style="color: #007700">));&nbsp;<br /><br /></span><span style="color: #FF8000">//&nbsp;Type&nbsp;3:&nbsp;Object&nbsp;method&nbsp;call<br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(array(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'myCallbackMethod'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;Type&nbsp;4:&nbsp;Static&nbsp;class&nbsp;method&nbsp;call&nbsp;(As&nbsp;of&nbsp;PHP&nbsp;5.2.3)<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(</span><span style="color: #DD0000">'MyClass::myCallbackMethod'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Type&nbsp;5:&nbsp;Relative&nbsp;static&nbsp;class&nbsp;method&nbsp;call&nbsp;(As&nbsp;of&nbsp;PHP&nbsp;5.3.0)<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;function&nbsp;</span><span style="color: #0000BB">who</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"A\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">B&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;function&nbsp;</span><span style="color: #0000BB">who</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"B\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'B'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'parent::who'</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//&nbsp;A<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-97">
    <p><strong>Example #2
     Callback example using a Closure
    </strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Our&nbsp;closure<br /></span><span style="color: #0000BB">$double&nbsp;</span><span style="color: #007700">=&nbsp;function(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />};<br /><br /></span><span style="color: #FF8000">//&nbsp;This&nbsp;is&nbsp;our&nbsp;range&nbsp;of&nbsp;numbers<br /></span><span style="color: #0000BB">$numbers&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Use&nbsp;the&nbsp;closure&nbsp;as&nbsp;a&nbsp;callback&nbsp;here&nbsp;to&nbsp;<br />//&nbsp;double&nbsp;the&nbsp;size&nbsp;of&nbsp;each&nbsp;element&nbsp;in&nbsp;our&nbsp;<br />//&nbsp;range<br /></span><span style="color: #0000BB">$new_numbers&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">$double</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$numbers</span><span style="color: #007700">);<br /><br />print&nbsp;</span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">'&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$new_numbers</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
2 4 6 8 10
</pre></div>
    </div>
   </div>
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>:
   <span class="simpara">
    In PHP 4, it was necessary to use a reference to create a callback that
    points to the actual <span class="type"><a href="language.types.object.php" class="type object">object</a></span>, and not a copy of it. For more
    details, see
    <a href="language.references.php" class="link">References Explained</a>.
   </span>
  </p></blockquote>

  <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">Callbacks registered
with functions such as  <span class="function"><a href="function.call-user-func.php" class="function">call_user_func()</a></span> and  <span class="function"><a href="function.call-user-func-array.php" class="function">call_user_func_array()</a></span> will not be
called if there is an uncaught exception thrown in a previous callback.</p></p></blockquote>
 </div>

</div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites