php-doc-en/language/types/callable.xml
2012-04-15 21:45:34 +00:00

173 lines
4.2 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<sect1 xml:id="language.types.callable">
<title>Callbacks</title>
<para>
Callbacks can be denoted by <type>callable</type> type hint as of PHP 5.4.
This documentation used <type>callback</type> type information for the same
purpose.
</para>
<para>
Some functions like <function>call_user_func</function> or
<function>usort</function> accept user-defined callback functions as a
parameter. Callback functions can not only be simple functions, but also
<type>object</type> methods, including static class methods.
</para>
<sect2 xml:id="language.types.callable.passing">
<title>Passing</title>
<para>
A PHP function is passed by its name as a <type>string</type>. Any built-in
or user-defined function can be used, except language constructs such as:
<function>array</function>, <function>echo</function>,
<function>empty</function>, <function>eval</function>,
<function>exit</function>, <function>isset</function>,
<function>list</function>, <function>print</function> or
<function>unset</function>.
</para>
<para>
A method of an instantiated <type>object</type> is passed as an
<type>array</type> containing an <type>object</type> at index 0 and the
method name at index 1.
</para>
<para>
Static class methods can also be passed without instantiating an
<type>object</type> of that class by passing the class name instead of an
<type>object</type> at index 0.
As of PHP 5.2.3, it is also possible to pass
<literal>'ClassName::methodName'</literal>.
</para>
<para>
Apart from common user-defined function, <function>create_function</function>
can also be used to create an anonymous callback function. As of PHP 5.3.0
it is possible to also pass a
<link linkend="functions.anonymous">closure</link> to a callback parameter.
</para>
<para>
<example>
<title>
Callback function examples
</title>
<programlisting role="php">
<![CDATA[
<?php
// An example callback function
function my_callback_function() {
echo 'hello world!';
}
// An example callback method
class MyClass {
static function myCallbackMethod() {
echo 'Hello World!';
}
}
// Type 1: Simple callback
call_user_func('my_callback_function');
// Type 2: Static class method call
call_user_func(array('MyClass', 'myCallbackMethod'));
// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));
// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');
// Type 5: Relative static class method call (As of PHP 5.3.0)
class A {
public static function who() {
echo "A\n";
}
}
class B extends A {
public static function who() {
echo "B\n";
}
}
call_user_func(array('B', 'parent::who')); // A
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>
Callback example using a Closure
</title>
<programlisting role="php">
<![CDATA[
<?php
// Our closure
$double = function($a) {
return $a * 2;
};
// This is our range of numbers
$numbers = range(1, 5);
// Use the closure as a callback here to
// double the size of each element in our
// range
$new_numbers = array_map($double, $numbers);
print implode(' ', $new_numbers);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
2 4 6 8 10
]]>
</screen>
</example>
</para>
<note>
<simpara>
In PHP 4, it was necessary to use a reference to create a callback that
points to the actual <type>object</type>, and not a copy of it. For more
details, see
<link linkend="language.references">References Explained</link>.
</simpara>
</note>
&note.func-callback-exceptions;
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->