Posted By

chrisaiv on 11/24/09


Tagged

php classexists


Versions (?)

PHP: Determine if a Class has been defined


 / Published in: PHP
 

  1. <?php
  2.  
  3. class Person
  4. {
  5. function say_hello(){
  6. echo "Yo Yo Yo";
  7. }
  8. }
  9.  
  10. /*
  11. //Reveal all the classes that have been defined including what PHP does by default
  12. $classes = get_declared_classes();
  13. foreach( $classes as $class ){
  14. echo $class . "<br />";
  15. }
  16. */
  17.  
  18. if( class_exists("Person") ){
  19. echo "That class has been defined.<br />";
  20. } else {
  21. echo "Class not defined!<br />";
  22. }
  23.  
  24. $methods = get_class_methods('Person');
  25. foreach($methods as $method){
  26. echo $method;
  27. }
  28.  
  29. if(method_exists('Person', 'say_hello')){
  30. echo "method does exist";
  31. } else {
  32. echo "Method does not exist";
  33. }
  34. ?>

Report this snippet  

You need to login to post a comment.