This is a discussion on PHP Function Handling Functions within the PHP Programming forums, part of the Web Development category; Hi, I want to know more on Function Handling functions in php. Can anyone explain me?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| call_user_func_array() call_user_func_array() call a user defined function given by function, with the parameters in param_arr. example PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| call_user_func() call_user_func() Call a user defined function given by the function parameter. example PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Object methods may also be invoked statically using this function by passing array($objectname, $methodname) to the function parameter. PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| the parameters for call_user_func() are not passed by reference. PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Some functions like call_user_func() or usort() accept user defined callback functions as a parameter. Callback functions can not only be simple functions but also object methods including static class methods. A PHP function is simply passed by its name as a string. You can pass any builtin or user defined function with the exception of array(), echo(), empty(), eval(), exit(), isset(), list(), print() and unset(). A method of an instantiated object is passed as an array containing an object as the element with index 0 and a method name as the element with index 1. Static class methods can also be passed without instantiating an object of that class by passing the class name instead of an object as the element with index 0.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Callback function examples PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| create_function() creates an anonymous function from the parameters passed, and returns a unique name for it. Usually the args will be passed as a single quote delimited string, and this is also recommended for the code. The reason for using single quoted strings, is to protect the variable names from parsing, otherwise, if you use double quotes there will be a need to escape the variable names, e.g. \$avar. You can use this function, to (for example) create a function from information gathered at run time
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| func_get_arg() returns the argument which is at the arg_num'th offset into a user-defined function's argument list. Function arguments are counted starting from zero. func_get_arg() will generate a warning if called from outside of a function definition. This function cannot be used directly as a function parameter. Instead, its result may be assigned to a variable, which can then be passed to the function. If arg_num is greater than the number of arguments actually passed, a warning will be generated and func_get_arg() will return FALSE.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| For example PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Because this function depends on the current scope to determine parameter details, it cannot be used as a function parameter. If you must pass this value, assign the results to a variable, and pass the variable.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| In the previous example func_num_args () returns the number of arguments passed into the current user-defined function. func_num_args() will generate a warning if called from outside of a user-defined function. This function cannot be used directly as a function parameter. Instead, its result may be assigned to a variable, which can then be passed to the function.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| func_get_args() returns a copy of the passed arguments only, and does not account for default (non-passed) arguments. Because this function depends on the current scope to determine parameter details, it cannot be used as a function parameter. If you must pass this value, assign the results to a variable, and pass the variable.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Here the example of calling func_get_args() PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| func_num_args() returns the number of arguments passed into the current user-defined function. func_num_args() will generate a warning if called from outside of a user-defined function. This function cannot be used directly as a function parameter. Instead, its result may be assigned to a variable, which can then be passed to the function. Because this function depends on the current scope to determine parameter details, it cannot be used as a function parameter. If you must pass this value, assign the results to a variable, and pass the variable.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| example for the above function is PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| function_exists() checks the list of defined functions, both built-in (internal) and user-defined, for function_name. example PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Note that a function name may exist even if the function itself is unusable due to configuration or compiling options (with the image functions being an example). Also note that function_exists() will return FALSE for constructs, such as include_once() and echo().
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| register_shutdown_function() registers the function named by function to be executed when script processing is complete.
__________________ With, J. Jeyaseelan Everything Possible |