Home > Article > Backend Development > PHP method to traverse all elements contained in a class_PHP tutorial
This example 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
?
2 3
4 56 |
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 => $valuen";
}
|
1 2 3 | TESTSTR1 => hello TESTVAR1 => 1001 TESTVAR2 => 1002 |