In this article, I am going to write about the functions using which you can get the information about php functions.
For a full list of PHP functions you can visit http://www.zend.com/phpfunc/
Using these functions you can query the stats/status of your functions, these functions are very useful in dynamic application creation.
For example, using func_get_arg and func_num_args functions you can simulate the function overloading in PHP.
Read a good example for http://www.dubi.org/php-function-overloading
Functions I will write in this article are :-
get_defined_functions();
You want to how many functions are in your installation of PHP, just call this function, it will return an array of all php functions you can call. For the convince it return an multidimensional array, so you can access both internal and user defined functions separately.
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.
As 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.
func_get_args()
Returns an array in which each element is a copy of the corresponding member of the current user-defined function’s argument list. func_get_args() 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.
This function returns a copy of the passed arguments only, and does not account for default (non-passed) arguments.
As 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.
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.
As func_get_arg() this function also can not be passed as argument to another function.
function_exists()
Checks the list of defined functions, both built-in (internal) and user-defined, for function_name. Returns TRUE on success or FALSE on failure.
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.
This is one of very useful function to create dynamic application, using this function you can create the function at run time and then use them as per your requirements.
call_user_func()
Call a user defined function given by the function parameter.
People are divided on the use of this function, some thinks that it is not needed at all when we can run the function on the variable itself.
Like use of this function is
function test($value) { print "This is $value"; } call_user_func("test","test value");
some people prefer using variable function like this :-
$funcname="test"; $funcname("test");
This is also a call_usr_func_array function.
is_callable()
If you are going to use a a variable function like previous example, you may need to verify if a function already present and callable? using this function you can do that.
This function Verify that the contents of a variable can be called as a function. This can check that a simple variable contains the name of a valid function, or that an array contains a properly encoded object and function name.
The var parameter can be either the name of a function stored in a string variable, or an object and the name of a method within the object, like this:
array($SomeObject, 'MethodName')
In some way it is similar to function_exisits function but it does more that that if you just want to check the syntax then you can pass a second argument as True, in that case it only verifies that var might be a function or method. It will only reject simple variables that are not strings, or an array that does not have a valid structure to be used as a callback. The valid ones are supposed to have only 2 entries, the first of which is an object or a string, and the second a string.
PHP is a functional language and you can almost do anything with these functions.