call_user_funcReturnsThe return value of function_name; if function_name is not a valid function name, the function generates a warning and no value is returned Descriptioncall_user_func() is primarily useful as a way to dynamically call functions and methods at runtime without having to use eval() . Normally, functions are called with the following syntax: function_name ('arg one', 'arg two', ...); Calling the same function using call_user_func() would look like this: call_user_func ('function_name', 'arg one', 'arg two', ...); The function name is somewhat of a misnomer - any PHP function can be called using this function. Watch out for language constructs like print() , echo() , unset() , etc. Attempts to call these constructs using this function will generate a warning.
TipTo call a method using this function, use the following syntax: $result = call_user_func (array (&$object, 'method'), $arg1, $arg2, ...); See alsoTo call an object method in the same manner: To pass a variable number of arguments to a function (or method): ExampleExample 378. Call a function using call_user_func() <pre> <?php $function_name = 'printf'; $format_string = "%-'.45s$%0.2f\n"; // Call printf using call_user_func call_user_func ($function_name, $format_string, "Total Cost", 1.1); // Make the same call to printf in the standard fashion printf ($format_string, "Total Cost", 1.1); // Make the same call to printf using the variable function behavior $function_name ($format_string, "Total Cost", 1.1); ?> </pre>
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|