How to method_exists() function to check if a class method exists in PHP

3 Answers

0 votes
class Test 
{
    var $var1 = "abc";
      
    public function show()
    {
        echo var1 . "<br />";
    }
}

$obj = new Test();

var_dump(method_exists($obj, 'show'));

/*
run:
 
bool(true) 
    
*/

 



answered Mar 15, 2016 by avibootz
0 votes
class Test 
{
    var $var1 = "abc";
      
    public function show()
    {
        echo var1 . "<br />";
    }
}

$obj = new Test();

if (method_exists($obj, 'show'))
    echo "method show() exists";

/*
run:
 
method show() exists  
    
*/

 



answered Mar 15, 2016 by avibootz
0 votes
$directory = new Directory('.');

$class_methods = get_class_methods($directory);
foreach ($class_methods as $method_name) 
      echo "Method: $method_name <br />";

if (method_exists($directory, 'read'))
    echo "<br />method read() exists";

/*
run:
 
Method: close
Method: rewind
Method: read

method read() exists 
    
*/

 



answered Mar 15, 2016 by avibootz
...