PhpDig.net

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




call_user_func_array

Name

call_user_func_array — Alternate method for calling functions.

Synopsis

mixed call_user_func_array(function_name[, argument_array]);
string function_name: Function to be called
array argument_array (optional): Array of arguments to be passed to function_name

Returns

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_array() is primarily useful as a way to dynamically call functions and methods at runtime without having to use eval() . call_user_func_array() passes the elements in argument_array to function_name as an argument list. This makes it easy to pass an unknown number of arguments to a function. See the example for details.

Normally, functions are called with the following syntax:

function_name ('arg one', 'arg two', ...);


Calling the same function using call_user_func_array() would look like this:

call_user_func_array ('function_name', array ('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 (array (&$object, 'method'), array ($arg1, $arg2, ...));




Version

PHP Version: 4.0.4+

See also

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

call_user_func()

func_get_arg()

func_get_args()

func_num_args()

Example

Example 377. Call a function using call_user_func_array()

<pre>
<?php
$function_name = 'printf';
$argv = array ("%-'.45s$%0.2f\n", "Total Cost", 1.1);

// Call printf using call_user_func_array
call_user_func_array ($function_name, $argv);
?>
</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.