Home > Backend Development > PHP Tutorial > php call_user_func function

php call_user_func function

WBOY
Release: 2016-07-29 09:13:02
Original
3692 people have browsed it

1. Function description

 Call the callback function. Prototype: mixed call_user_func ( callable callback[ ,mix edparameter [, mixed $… ]] ). The first parameter callback is the callback function being called, and the remaining parameters are the parameters of the callback function. There can be multiple parameters or an array.
Parameters cannot be references, but references can be passed through arrays. Here is an example:

<code><span><?php</span><span><span>function</span><span>addone</span><span>(&<span>$num</span>)</span> {</span><span>$num</span> ++; 
}

<span>$num</span> = <span>0</span>;

call_user_func(<span>'addone'</span>, <span>$num</span>);
<span>echo</span><span>"num : "</span> . <span>$num</span> . <span>"\n"</span>;

call_user_func_array(<span>'addone'</span>, <span>array</span>(&<span>$num</span>) );
<span>echo</span><span>"num : "</span> . <span>$num</span> . <span>"\n"</span>;

<span>?></span></code>
Copy after login

Output:
0
1
Return value: Return the return value of the callback function, or FALSE if there is an error.

2. Example

  • Usage of namespace.
<code><span><?php</span><span>namespace</span><span>Foobar</span>;

<span><span>class</span><span>Foo</span> {</span><span>static</span><span>public</span><span><span>function</span><span>test</span><span>()</span> {</span><span>print</span><span>"Hello world!\n"</span>;
    }
}

call_user_func(<span>__NAMESPACE__</span> .<span>'\Foo::test'</span>); 
call_user_func(<span>array</span>(<span>__NAMESPACE__</span> .<span>'\Foo'</span>, <span>'test'</span>)); 

<span>?></span></code>
Copy after login
  • Call a method in a class
<code><span><?php</span><span><span>class</span><span>myclass</span> {</span><span>static</span><span><span>function</span><span>test</span><span>()</span>
    {</span><span>echo</span><span>"Hello world!\n"</span>;
    }
}

<span>$classname</span> = <span>"myclass"</span>;

call_user_func(<span>array</span>(<span>$classname</span>, <span>'test'</span>));

<span>$myobject</span> = <span>new</span> myclass();

call_user_func(<span>array</span>(<span>$myobject</span>, <span>'test'</span>));

<span>?></span></code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the php call_user_func function, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template