slimboy007 Posted June 17, 2010 Share Posted June 17, 2010 I have a question which i am not sure how to answer can anyone help please? Using PHP, determine whether the method 'simple_sum' exists in the class Exam. class Exam{ public function simple_sum(){ return null; } } Link to comment https://forums.phpfreaks.com/topic/205064-does-this-exist-in-a-class/ Share on other sites More sharing options...
jonsjava Posted June 17, 2010 Share Posted June 17, 2010 sounds like schoolwork. No cheating. Link to comment https://forums.phpfreaks.com/topic/205064-does-this-exist-in-a-class/#findComment-1073479 Share on other sites More sharing options...
bugcoder Posted June 17, 2010 Share Posted June 17, 2010 <?php class myclass { // constructor function myclass() { return(true); } // method 1 function myfunc1() { return(true); } // method 2 function myfunc2() { return(true); } } $class_methods = get_class_methods('myclass'); // or $class_methods = get_class_methods(new myclass()); foreach ($class_methods as $method_name) { echo "$method_name\n"; } ?> The above example will output: myclass myfunc1 myfunc2 Link to comment https://forums.phpfreaks.com/topic/205064-does-this-exist-in-a-class/#findComment-1073485 Share on other sites More sharing options...
jonsjava Posted June 17, 2010 Share Posted June 17, 2010 dirty way. Ok, I'll show the proper method: <?php class test{ public function test2(){ echo "hello"; } } $test = new test(); if (method_exists(test,test2)){ echo "found method"; } ?> Link to comment https://forums.phpfreaks.com/topic/205064-does-this-exist-in-a-class/#findComment-1073486 Share on other sites More sharing options...
Alex Posted June 17, 2010 Share Posted June 17, 2010 dirty way. Ok, I'll show the proper method: <?php class test{ public function test2(){ echo "hello"; } } $test = new test(); if (method_exists(test,test2)){ echo "found method"; } ?> The first parameter has to either be a string of name of the class you're testing or either an instance of the class. So in your example either 'test' or $test. The second parameter has to be a string, 'test2'. if (method_exists($test, 'test2')){ echo "found method"; } or if (method_exists('test', 'test2')){ echo "found method"; } Link to comment https://forums.phpfreaks.com/topic/205064-does-this-exist-in-a-class/#findComment-1073491 Share on other sites More sharing options...
jonsjava Posted June 17, 2010 Share Posted June 17, 2010 The first parameter has to either be a string of name of the class you're testing or either an instance of the class. So in your example either 'test' or $test. The second parameter has to be a string, 'test2'. if (method_exists($test, 'test2')){ echo "found method"; } or if (method_exists('test', 'test2')){ echo "found method"; } Forgot to mention that. Thanks, Alex. Link to comment https://forums.phpfreaks.com/topic/205064-does-this-exist-in-a-class/#findComment-1073493 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.