2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 07:07:53 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-06-20 22:25:43 +00:00
|
|
|
<refentry xml:id="function.call-user-func" xmlns="http://docbook.org/ns/docbook">
|
2007-02-17 20:02:43 +00:00
|
|
|
<refnamediv>
|
|
|
|
<refname>call_user_func</refname>
|
2011-12-16 16:22:40 +00:00
|
|
|
<refpurpose>Call the callback given by the first parameter</refpurpose>
|
2007-02-17 20:02:43 +00:00
|
|
|
</refnamediv>
|
2007-02-17 20:03:06 +00:00
|
|
|
|
|
|
|
<refsect1 role="description">
|
|
|
|
&reftitle.description;
|
2007-02-17 20:02:43 +00:00
|
|
|
<methodsynopsis>
|
|
|
|
<type>mixed</type><methodname>call_user_func</methodname>
|
2012-03-12 05:55:59 +00:00
|
|
|
<methodparam><type>callable</type><parameter>callback</parameter></methodparam>
|
2007-02-17 20:02:43 +00:00
|
|
|
<methodparam choice="opt"><type>mixed</type><parameter>parameter</parameter></methodparam>
|
|
|
|
<methodparam choice="opt"><type>mixed</type><parameter>...</parameter></methodparam>
|
|
|
|
</methodsynopsis>
|
|
|
|
<para>
|
2011-12-16 16:22:40 +00:00
|
|
|
Calls the <parameter>callback</parameter> given by the first parameter and passes
|
|
|
|
the remaining parameters as arguments.
|
2007-02-17 20:03:06 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="parameters">
|
|
|
|
&reftitle.parameters;
|
|
|
|
<para>
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
2011-12-16 16:22:40 +00:00
|
|
|
<term><parameter>callback</parameter></term>
|
2007-02-17 20:03:06 +00:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2012-03-12 05:55:59 +00:00
|
|
|
The <type>callable</type> to be called.
|
2007-02-17 20:03:06 +00:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>parameter</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2011-12-16 16:22:40 +00:00
|
|
|
Zero or more parameters to be passed to the callback.
|
2007-02-17 20:03:06 +00:00
|
|
|
</para>
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
Note that the parameters for <function>call_user_func</function> are
|
|
|
|
not passed by reference.
|
2009-07-02 21:52:02 +00:00
|
|
|
<example>
|
|
|
|
<title><function>call_user_func</function> example and references</title>
|
2007-02-17 20:03:06 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2009-07-02 21:52:02 +00:00
|
|
|
error_reporting(E_ALL);
|
2007-02-17 20:03:06 +00:00
|
|
|
function increment(&$var)
|
|
|
|
{
|
|
|
|
$var++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = 0;
|
|
|
|
call_user_func('increment', $a);
|
2009-07-02 21:52:02 +00:00
|
|
|
echo $a."\n";
|
2007-02-17 20:03:06 +00:00
|
|
|
|
2014-11-17 20:47:41 +00:00
|
|
|
// You can use this instead
|
|
|
|
call_user_func_array('increment', array(&$a));
|
2009-07-02 21:52:02 +00:00
|
|
|
echo $a."\n";
|
2007-02-17 20:03:06 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
2009-07-02 21:52:02 +00:00
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
2018-06-24 13:18:06 +00:00
|
|
|
Warning: Parameter 1 to increment() expected to be a reference, value given in …
|
2009-07-02 21:52:02 +00:00
|
|
|
0
|
|
|
|
1
|
|
|
|
]]>
|
|
|
|
</screen>
|
|
|
|
</example>
|
2007-02-17 20:03:06 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="returnvalues">
|
|
|
|
&reftitle.returnvalues;
|
|
|
|
<para>
|
2016-09-19 16:19:13 +00:00
|
|
|
Returns the return value of the callback.
|
2007-02-17 20:02:43 +00:00
|
|
|
</para>
|
2007-02-17 20:03:06 +00:00
|
|
|
</refsect1>
|
|
|
|
|
2010-10-20 07:11:18 +00:00
|
|
|
<refsect1 role="changelog">
|
|
|
|
&reftitle.changelog;
|
|
|
|
<para>
|
|
|
|
<informaltable>
|
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
|
|
|
<row>
|
|
|
|
<entry>&Version;</entry>
|
|
|
|
<entry>&Description;</entry>
|
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<row>
|
|
|
|
<entry>5.3.0</entry>
|
|
|
|
<entry>
|
|
|
|
The interpretation of object oriented keywords like <literal>parent</literal>
|
2010-10-24 12:49:58 +00:00
|
|
|
and <literal>self</literal> has changed. Previously, calling them using the
|
2010-10-20 07:11:18 +00:00
|
|
|
double colon syntax would emit an <constant>E_STRICT</constant> warning because
|
2010-10-24 12:49:58 +00:00
|
|
|
they were interpreted as static.
|
2010-10-20 07:11:18 +00:00
|
|
|
</entry>
|
|
|
|
</row>
|
2018-06-24 13:18:06 +00:00
|
|
|
<row>
|
|
|
|
<entry>5.3.0</entry>
|
|
|
|
<entry>
|
|
|
|
If the called function expects a parameter to be passed by reference, an
|
|
|
|
<constant>E_WARNING</constant> is now issued.
|
|
|
|
</entry>
|
|
|
|
</row>
|
2010-10-20 07:11:18 +00:00
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</informaltable>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
2007-02-17 20:03:06 +00:00
|
|
|
<refsect1 role="examples">
|
|
|
|
&reftitle.examples;
|
2007-02-17 20:02:43 +00:00
|
|
|
<para>
|
2007-02-17 20:03:06 +00:00
|
|
|
<example>
|
|
|
|
<title><function>call_user_func</function> example</title>
|
2007-02-17 20:02:43 +00:00
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2003-06-16 14:37:37 +00:00
|
|
|
<?php
|
2007-02-17 20:02:43 +00:00
|
|
|
function barber($type)
|
2004-01-15 12:43:50 +00:00
|
|
|
{
|
2009-07-02 21:52:02 +00:00
|
|
|
echo "You wanted a $type haircut, no problem\n";
|
2002-04-15 00:12:54 +00:00
|
|
|
}
|
2003-12-15 16:55:22 +00:00
|
|
|
call_user_func('barber', "mushroom");
|
|
|
|
call_user_func('barber', "shave");
|
2008-01-23 19:06:10 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
2009-07-02 21:52:02 +00:00
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
|
|
|
You wanted a mushroom haircut, no problem
|
|
|
|
You wanted a shave haircut, no problem
|
|
|
|
]]>
|
|
|
|
</screen>
|
2008-01-23 19:06:10 +00:00
|
|
|
</example>
|
|
|
|
<example>
|
|
|
|
<title><function>call_user_func</function> using namespace name</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Foobar;
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
static public function test() {
|
|
|
|
print "Hello world!\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-02 01:51:02 +00:00
|
|
|
call_user_func(__NAMESPACE__ .'\Foo::test'); // As of PHP 5.3.0
|
|
|
|
call_user_func(array(__NAMESPACE__ .'\Foo', 'test')); // As of PHP 5.3.0
|
2008-01-23 19:06:10 +00:00
|
|
|
|
2003-06-16 14:37:37 +00:00
|
|
|
?>
|
2003-01-28 21:06:51 +00:00
|
|
|
]]>
|
2007-02-17 20:02:43 +00:00
|
|
|
</programlisting>
|
2009-07-02 21:52:02 +00:00
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
|
|
|
Hello world!
|
|
|
|
Hello world!
|
|
|
|
]]>
|
|
|
|
</screen>
|
2007-02-17 20:03:06 +00:00
|
|
|
</example>
|
|
|
|
<example>
|
2009-07-02 21:52:02 +00:00
|
|
|
<title>Using a class method with <function>call_user_func</function></title>
|
2007-02-17 20:02:43 +00:00
|
|
|
<programlisting role="php">
|
2003-01-28 21:06:51 +00:00
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2008-01-25 00:02:31 +00:00
|
|
|
|
2003-01-28 21:06:51 +00:00
|
|
|
class myclass {
|
2008-01-25 00:02:31 +00:00
|
|
|
static function say_hello()
|
2004-08-06 22:16:44 +00:00
|
|
|
{
|
|
|
|
echo "Hello!\n";
|
|
|
|
}
|
2003-01-28 21:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$classname = "myclass";
|
|
|
|
|
2003-12-15 16:55:22 +00:00
|
|
|
call_user_func(array($classname, 'say_hello'));
|
2008-01-25 00:02:31 +00:00
|
|
|
call_user_func($classname .'::say_hello'); // As of 5.2.3
|
|
|
|
|
2009-05-04 22:20:08 +00:00
|
|
|
$myobject = new myclass();
|
|
|
|
|
|
|
|
call_user_func(array($myobject, 'say_hello'));
|
|
|
|
|
2008-08-29 12:47:25 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
2009-07-02 21:52:02 +00:00
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
|
|
|
Hello!
|
|
|
|
Hello!
|
|
|
|
Hello!
|
|
|
|
]]>
|
|
|
|
</screen>
|
2008-08-29 12:47:25 +00:00
|
|
|
</example>
|
|
|
|
<example>
|
2009-07-02 21:52:02 +00:00
|
|
|
<title>Using lambda function with <function>call_user_func</function></title>
|
2008-08-29 12:47:25 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
call_user_func(function($arg) { print "[$arg]\n"; }, 'test'); /* As of PHP 5.3.0 */
|
2003-01-28 21:06:51 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2007-02-17 20:02:43 +00:00
|
|
|
</programlisting>
|
2009-07-02 21:52:02 +00:00
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
|
|
|
[test]
|
|
|
|
]]>
|
|
|
|
</screen>
|
2007-02-17 20:03:06 +00:00
|
|
|
</example>
|
2007-02-17 20:02:43 +00:00
|
|
|
</para>
|
2007-02-17 20:03:06 +00:00
|
|
|
</refsect1>
|
2004-08-06 22:16:44 +00:00
|
|
|
|
2010-01-13 02:09:45 +00:00
|
|
|
<refsect1 role="notes">
|
|
|
|
&reftitle.notes;
|
|
|
|
¬e.func-callback-exceptions;
|
|
|
|
</refsect1>
|
|
|
|
|
2007-02-17 20:03:06 +00:00
|
|
|
<refsect1 role="seealso">
|
|
|
|
&reftitle.seealso;
|
2007-02-17 20:02:43 +00:00
|
|
|
<para>
|
2007-02-17 20:03:06 +00:00
|
|
|
<simplelist>
|
|
|
|
<member><function>call_user_func_array</function></member>
|
|
|
|
<member><function>is_callable</function></member>
|
|
|
|
<member>&seealso.callback;</member>
|
2012-01-07 23:18:24 +00:00
|
|
|
<member><methodname>ReflectionFunction::invoke</methodname></member>
|
|
|
|
<member><methodname>ReflectionMethod::invoke</methodname></member>
|
2007-02-17 20:03:06 +00:00
|
|
|
</simplelist>
|
2007-02-17 20:02:43 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
2007-02-17 20:03:06 +00:00
|
|
|
|
2007-02-17 20:02:43 +00:00
|
|
|
</refentry>
|
2002-04-15 00:12:54 +00:00
|
|
|
|
|
|
|
<!-- 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
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2002-04-15 00:12:54 +00:00
|
|
|
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
|
|
|
|
-->
|