To check if the class method exists or not we use a function method_exists() in PHP. It check that the class method exist in the given object.
Syntax:
method_exists($object,$method);
As you have seen the syntax of method_exists() above, it includes two parameters:
- $object is the object instance or class name
- $method is the name of the function you want to check.
For example:
<?php
class ParentClass {
function demo() { }
}
class ChildClass extends ParentClass { }
$p = new ParentClass();
$c = new ChildClass();
// all return true
if(method_exists($p, 'demo')){
echo "demo exist in parent";
}
else
{
echo "demo does not exist in parent";
}
?>
Output of above script is as follows:
demo exist in parent
0 Comment(s)