PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




call_user_func

Name

call_user_func — An alternate method for calling functions.

Synopsis

mixed call_user_func(function_name[, function_parameter][, . . .]);
string function_name: Function to be called
mixed function_parameter (optional): First argument to be passed to function_name
mixed . . . (optional): Additional arguments to be passed to function_name

Returns

The return value of function_name; if function_name is not a valid function name, the function generates a warning and no value is returned

Description

call_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.

Tip

To call a method using this function, use the following syntax:

$result = call_user_func (array (&$object, 'method'), $arg1, $arg2, ...);




Version

PHP Version: 3.0.3+, 4+

See also

To call an object method in the same manner:

call_user_method()

To pass a variable number of arguments to a function (or method):

call_user_func_array()

func_get_arg()

func_get_args()

func_num_args()

Example

Example 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.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.