Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to check a class method exist?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 454
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: