PHP | is_callable() Function

The is_callable() function is an inbuilt function in PHP which is used to verify the contents of a variable can be 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 ( $variable_name, $syntax_only, $callable_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.



  • $variable_name: The name of a function stored in a string variable $variable_name, or an object and the name of a method within the object.
  • $syntax_only: If 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.
  • $callable_name: Receives the callable name. This option only implemented for classes.

Return value: This function returns a boolean type value. It returns TRUE if $variable_name is callable, FALSE otherwise.

Below program illustrate the is_callable() function in PHP:
Program 1: Simple variable contains a function

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// To check a variable if it can be called
// as a function.
  
// Declare the function
function Function_xyz() 
{
}
  
$variable_name = "Function_xyz";
var_dump(is_callable($variable_name, false, $callable_name));
echo $callable_name, "\n";
  
// using only-one parameter
var_dump(is_callable($variable_name));
?>

chevron_right


Output:

bool(true)
Function_xyz
bool(true)

Program 2: Array contains a method

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// To check a variable if it can be called
// as a function.
  
// Define class
class ClassA {
   // Define method
  function Method_xyz() 
  {
  }
  
}
  
// Object instance
$obj = new ClassA();
  
$variable_name = array($obj, 'Method_xyz');
  
var_dump(is_callable($variable_name, true, $callable_name));  
echo $callable_name, "\n";  
?>

chevron_right


Output:

bool(true)
ClassA::Method_xyz

Reference: http://php.net/manual/en/function.is-callable.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.