- Using a function is_callable() :is a built-in function in PHP that is used to check the contents of a variable being called as a function. It can check that a simple variable contains the name of a valid function, or that an array contains a correctly encoded object and a function name. Syntax:
bool is_callable ($var_name, $syntx_only, $calbl_name)
Parameters:The is_callable() function takes three parameters as shown in the above syntax and is described below. It is up to the user to use, how many parameters are one, two or three.- $var_name:the name of the function stored in the string variable $var_name, or an object and name method inside an object.
- $syntx_only:if set to TRUE, the function only checks that the name can be a function or a method. It will reject simple variables that are not strings, or an array that has no valid structure to use as a callback. The valid ones should only have 2 records, the first one being an object or string and the second one - string.
- $calbl_name:gets the name to call. This option is only available for classes.
// Declare the variable and initialize
// with function
$function
=
function
() {
echo
’GeeksForGeeks’
;
};
// Function check is_callable contains
// function or not
if
(
is_callable
(
$function
)) {
echo
"It is function "
;
}
else
{
echo
" It is not function "
;
}
// Declare a variable and
// initialize this
$var
=
"GeeksForGeeks"
;
echo
""
;
// Function check is_callable contains
// function or not
if
(
is_callable
(
$var
)) {
echo
"It is function "
;
}
else
{
echo
" It is not function "
;
}
?>
It is function It is not function
- instanceof:PHP’s instanceof operator is used to find out if an object is an instance of a class. Syntax:
$f instanceof Class_Name
Operands:it contains two operands, which are listed below:- $f:is used as an object.
- Class_Name:is used to store the class name.
// Declare a variable and initialize
// with function
$func
=
function
() {
echo
’GeeksforGeeks’
;
};
// Use instanceof to check that it contains
// function or not
if
(
$func
instanceof
Closure) {
echo
"function"
;
}
else
{
echo
" not a function "
;
}
// Declare a variable and initialize it
$var
=
"GFG"
;
echo
""
;
// Use instanceof to check that it contains
// function or not
if
(
$var
instanceof
Closure) {
echo
"function"
;
}
else
{
echo
" not a function "
;
}
?>
function not a function
- Example:This example uses the methods function_exist() and is_object() to check if the argument is a function or not.
// Declare the function
function
myFun() {
echo
’GeeksforGeeks’
;
};
// Determine if the variable is a function
function
is_function (
$func
) {
return
(
is_string
(
$func
) & amp; & amp; function_exists (
$func
))
|| (
is_object
(
$func
) & amp; & amp; (
$func
instanceof
Closure) );
}
echo
is_function (
’myFun’
);
?>
1