Php is callable function

PHP is_callable() Function

Check whether the contents of a variable can be called as a function or not:

echo «test1 is callable: » . is_callable(«test1»);
echo «
«;
echo «test2 is callable: » . is_callable(«test2»);
?>

Definition and Usage

The is_callable() function checks whether the contents of a variable can be called as a function or not.

This function returns true (1) if the variable is callable, otherwise it returns false/nothing.

Syntax

Parameter Values

Parameter Description
variable Required. Specifies the variable to check
syntax_only Optional. If set to TRUE, the function only verifies if variable is a function or method. It will reject variables that are not strings, or arrays without a valid structure to be used as a callback. Default is false
name Optional. Returns a «callable name» (only for classes)

Technical Details

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

How to Use is_callable() Function in PHP

PHP has a helpful function called is_callable() that determines whether a particular function or method exists and may be called. It is especially useful in object-oriented programming (OOP) where a lot of a program’s logic and behavior are encapsulated within classes and methods. Checking whether a certain method exists and is callable before using it within the program can help prevent unexpected errors.

What is is_callable() Function in PHP

In PHP, the is_callable() function returns a Boolean value which indicates whether the passed function or method can be called.

The syntax to use the is_callable() function in PHP is given below:

It accepts three parameters:

  • $function: It is the name of the function or method.
  • $syntax_only: Optional. If TRUE is specified, the function just checks to see if the given name is a function or method. Simple variables that are neither strings nor arrays without a proper structure for a callback will be rejected. The only two entries that make up a legitimate one should be an object or a string as the first entry and a string as the second.
  • $callable_name: Optional. Returns a ‘callable name’.

How to Use is_callable() Function in PHP?

To use the is_callable() function in PHP, first, we need to understand which type of callable functions or methods can be evaluated using this function. It can evaluate a simple function or a static method of a class.

One thing to note is that it is possible to pass the name of a function as a string to the is_callable() function without calling the function. This is because PHP treats functions and methods as first-class citizens. As such, we can pass the name of a function or method as a string, and it will be evaluated as if it were called.

echo «The function is callable.» ;

echo «The function is not callable.» ;

The function keyword is used in the above code to define the function “myFunction”. The “Linuxhint!” string is just echoed by the function. The is_callable() method is then used by the code to determine whether the function “article” is callable. The is_callable() function returns yes since the function has been declared.

According to the outcome of the is_callable() check, the code utilizes an if-else statement to output either “The function is callable.” or “The function is not callable.”.

Another thing to note is that if the second parameter is set to true, the function will only check if the callable syntax is valid and not whether the function or method exists. However, if it is set to false, it will check if the callable syntax is valid and also ensure that the function or method exists.

echo «The function is callable.» ;

echo «The function is not callable.» ;

The function keyword is used in the above code to define the function “article”. The “Linuxhint!” string is just echoed by the function. The is_callable() method is then used by the code to determine whether the function “article” is callable. To deactivate strict verification for the function’s existence, the second parameter of the is_callable() function is set to false.

According to the outcome of the is_callable() check, the code utilizes an if-else statement to output either “The function is callable” or “The function is not callable”.

In OOP, we can take advantage of the is_callable() function to check if a method is callable before calling it. For instance, in a class method, we may use the is_callable() function to determine whether a specific method is callable and present in the class before invoking it. If the method is missing, this might assist stop the application from throwing an error.

$var_name = array ( $obj , ‘linuxhint’ ) ;

Within the class “article,” the code above specifies the methods “linuxhint” and “article”. The article class object is then created using the $obj variable. The array($obj, ‘linuxhint’) syntax is used to assign the method “linuxhint” as a callable to the $var_name variable in the next line.

The is_callable() method is then used in the code to determine whether the variable $var_name is callable. The is_callable() check’s Boolean outcome is shown using the var_dump() function. Finally, the code uses the is_callable() method with the third argument $callable_name to attempt to extract the name of the callable. The value of $callable_name is then printed.

Conclusion

When analyzing callable functions or methods in PHP, the is_callable() function is a helpful function. It helps prevent unexpected errors by checking whether a function or method exists before calling it. As an added advantage, it can be utilized in OOP when writing class methods to check if a particular method exists in the class before calling it.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.

Источник

is_callable

is_callable — Verify that a value can be called as a function from the current scope.

Description

is_callable ( mixed $value , bool $syntax_only = false , string &$callable_name = null ): bool

Parameters

If set to true the function only verifies that value 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.

Receives the «callable name». In the example below it is «someClass::someMethod». Note, however, that despite the implication that someClass::SomeMethod() is a callable static method, this is not the case.

Return Values

Returns true if value is callable, false otherwise.

Examples

Example #1 is_callable() example

// How to check a variable to see if it can be called
// as a function.

//
// Simple variable containing a function
//

var_dump ( is_callable ( $functionVariable , false , $callable_name )); // bool(true)

echo $callable_name , «\n» ; // someFunction

//
// Array containing a method
//

$methodVariable = array( $anObject , ‘someMethod’ );

var_dump ( is_callable ( $methodVariable , true , $callable_name )); // bool(true)

echo $callable_name , «\n» ; // someClass::someMethod

Example #2 is_callable() and constructors

is_callable() reports constructors as not being callable.

class Foo
public function __construct () <>
public function foo () <>
>

var_dump (
is_callable (array( ‘Foo’ , ‘__construct’ )),
is_callable (array( ‘Foo’ , ‘foo’ ))
);

The above example will output:

Notes

  • An object is always callable if it implements __invoke(), and that method is visible in the current scope.
  • A class name is callable if it implements __callStatic().
  • If an object implements __call(), then this function will return true for any method on that object, even if the method is not defined.
  • This function may trigger autoloading if called with the name of a class.

See Also

  • function_exists() — Return true if the given function has been defined
  • method_exists() — Checks if the class method exists

User Contributed Notes 7 notes

If the target class has __call() magic function implemented, then is_callable will ALWAYS return TRUE for whatever method you call it.
is_callable does not evaluate your internal logic inside __call() implementation (and this is for good).
Therefore every method name is callable for such classes.

Hence it is WRONG to say (as someone said):
. is_callable will correctly determine the existence of methods made with __call.

Example:
class TestCallable
public function testing ()
return «I am called.» ;
>

public function __call ( $name , $args )
if( $name == ‘testingOther’ )
return call_user_func_array (array( $this , ‘testing’ ), $args );
>
>
>

$t = new TestCallable ();
echo $t -> testing (); // Output: I am called.
echo $t -> testingOther (); // Output: I am called.
echo $t -> working (); // Output: (null)

echo is_callable (array( $t , ‘testing’ )); // Output: TRUE
echo is_callable (array( $t , ‘testingOther’ )); // Output: TRUE
echo is_callable (array( $t , ‘working’ )); // Output: TRUE, expected: FALSE
?>

Note that — as mentioned in the migration guide— starting from PHP 8.0, is_callable() will not work with non-static methods if you use a class name, instead an object of the class should be provided:

class Test
public function method1 () < >
public static function method2 () < >
>

// Pre PHP 8
var_dump ( is_callable (array( ‘Test’ , ‘method1’ ))); // bool(true)
var_dump ( is_callable (array( ‘Test’ , ‘method2’ ))); // bool(true)

// Post PHP 8
var_dump ( is_callable (array( ‘Test’ , ‘method1’ ))); // bool(false)
var_dump ( is_callable (array( ‘Test’ , ‘method2’ ))); // bool(true)
var_dump ( is_callable (array(new Test , ‘method1’ ))); // bool(true)

The story about __call() is a bit more complicated unfortunately. It will always return true ONLY if you pass an instance of a class, not if you pass the class name itself:

class MyClass public function method () < >
public function __call ( $name , $arguments ) < >
>

is_callable ([ MyClass ::class, ‘method’ ]); // true
is_callable ([new MyClass (), ‘method’ ]); // true
is_callable ([ MyClass ::class, ‘other’ ]); // false.
is_callable ([new MyClass (), ‘other’ ])); // true

is_callable generates an [E_STRICT] error if the tested method cannot be called staticly. (and returns the good value)

I used @is_called
i’m using php 5.2.1

is_callable() does _not_ check wheter this function is disabled by php.ini’s disable_functions

function is_disabled ( $function ) $disabled_functions = explode ( ‘,’ , ini_get ( ‘disable_functions’ ));
return in_array ( $function , $disabled_functions );
>
?>

I`m running PHP 5.2.4

Note that is_callable(‘_’) will return true, since there is a built-in function called «_». It is an alias for gettext().

Searching this site for a function named «_» does not return a result, but it’s the first result in a search for «_()».

Note that, for the purpose of this function, an abstract method, although necessarily non-callable since it does not have a body, is still considered to be callable:

abstract class Foo <
abstract function bar ();
>

echo is_callable (array( ‘Foo’ , ‘bar’ ));
// display: 1
?>

Источник

Читайте также:  Java xml with attributes
Оцените статью