The example in this article describes the method of php traversing all elements contained in a class. Share it with everyone for your reference. The specific analysis is as follows:
Here you can get all the elements contained in the php class to be output in the form of key-value
class MyTestClass{ const TESTVAR1 = 1001; const TESTVAR2 = 1002; const TESTSTR1 = 'hello'; } $rc = new ReflectionClass('MyTestClass'); $v = $rc->getConstants(); asort($v);// sort by value //ksort($v);// sort by key foreach ( $v as $name => $value){ echo "$name => $value\n"; }
The running results are as follows:
TESTSTR1 => hello TESTVAR1 => 1001 TESTVAR2 => 1002
I hope this article will be helpful to everyone’s PHP programming design.