PHP method_exists() Function
The PHP method_exists() function is used to check if the given method exists in the specified object_or_class. It returns true if the given method exists for the given object_or_class, false otherwise.
Syntax
method_exists(object_or_class, method)
Parameters
object_or_class |
Required. Specify the class name or an object of the class to check for. |
method |
Required. Specify the name of the method. |
Return Value
Returns true if the given method has been defined for the given object_or_class, false otherwise.
Example: method_exists() example
The example below shows the usage of method_exists() function.
<?php class myClass { public $mine; private $xpto; static protected $test; static function test() { echo "Hello World."; } } var_dump(method_exists('myClass', 'mine')); var_dump(method_exists('myClass', 'xpto')); var_dump(method_exists('myClass', 'bar')); var_dump(method_exists('myClass', 'test')); ?>
The output of the above code will be:
bool(false) bool(false) bool(false) bool(true)
Example: method_exists() example
Consider one more example which illustrates on usage of method_exists() function.
<?php $directory = new Directory('.'); var_dump(method_exists($directory,'read')); var_dump(method_exists('Directory','read')); ?>
The output of the above code will be:
bool(true) bool(true)
❮ PHP Classes/Objects Reference