To determine whether a passed argument is a function or not, Few of the most preferred methods are shown below.
- Using is_callable() Function: It is an inbuilt function in PHP which is used to verify the contents of a variable in called as a function. It 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.
Syntax:
bool is_callable ( $var_name, $syntx_only, $calbl_name )
Parameters: The is_callable() function accepts three parameters as shown in above syntax and are described below. It depends on user to use how many parameters one, two or three.
- $var_name: The name of a function stored in a string variable $var_name, or an object and the name of a method within the object.
- $syntx_only: If it set to TRUE the function only verifies that name might be a function or method. It will 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.
- $calbl_name: It receives the callable name. This option only implemented for classes.
Return Value: This function returns a boolean type value. It returns TRUE if $var_name is callable, FALSE otherwise.
Example: This example uses is_callable() function to verify whether the parameter is a function or not.
<?phpÂÂ// Declare a variable and initialize// with function$function=function() {ÂÂ Â Â Âecho'GeeksForGeeks';Â};ÂÂ// Check is_callable function contains// function or notif(is_callable($function) ) {Â Â Â Âecho"It is function";}else{Â Â Â Âecho"It is not function";}ÂÂ// Declare a variable andÂ// initialize it$var="GeeksForGeeks";echo"\n";ÂÂ// Check is_callable function contains// function or notif(is_callable($var) ) {Â Â Â Âecho"It is function";}else{Â Â Â Âecho"It is not function";}ÂÂ?>Output:It is function It is not function
- instanceof: The instanceof operator in PHP is used to find out if an object is an instantiated instance of a class.
Syntax:
$f instanceof Class_Name
Operands: It contains two operands which are listed below:
- $f: It is used as an object.
- Class_Name: It is used to hold the class name.
Return Value: It returns True if the object is of this class or has this class as one of its parents else it will return a False value.
Example: This example use instanceof operator to determine if a variable is a function in PHP.
<?phpÂÂ// Declare a variable and initialize// with function$func=function() {ÂÂ Â Â Âecho'neveropen';Â};ÂÂ// Use instanceof to check it contains// function or notif($funcinstanceofClosure) {Â Â Â Âecho"function";}else{Â Â Â Âecho"not a function";}ÂÂ// Declare a variable and initialize it$var="GFG";echo"\n";ÂÂ// Use instanceof to check it contains// function or notif($varinstanceofClosure) {Â Â Â Âecho"function";}else{Â Â Â Âecho"not a function";}?>Output:function not a function
- Example: This example uses function_exist() and is_object() methods to check whether an argument is a function or not.
<?phpÂÂ// Declare a functionfunctionmyFun() {ÂÂ Â Â Âecho'neveropen';Â};ÂÂÂ// Determine if a variable is a functionfunctionis_function($func) {Â Â Â Âreturn(is_string($func) && function_exists($func))ÂÂ Â Â Â|| (is_object($func) && ($funcinstanceofClosure));}ÂÂechois_function('myFun');ÂÂ?>Output:1
