Get a list of class constants


/ Published in: PHP
Save to your folder(s)

Gets the constants from a class using reflection.


Copy this code and paste it in your HTML
  1. class MyTestClass{
  2. const TESTVAR1 = 1001;
  3. const TESTVAR2 = 1002;
  4. const TESTSTR1 = 'hello';
  5. }
  6.  
  7.  
  8. $rc = new ReflectionClass('MyTestClass');
  9. $v = $rc->getConstants();
  10.  
  11. asort($v);// sort by value
  12. //ksort($v);// sort by key
  13.  
  14. foreach ( $v as $name => $value){
  15. echo "$name => $value\n";
  16. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.