Home >Backend Development >PHP Tutorial >PHP method to traverse all elements contained in a class, php_PHP tutorial

PHP method to traverse all elements contained in a class, php_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 09:54:051071browse

php method of traversing all elements contained in a class, php

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/998570.htmlTechArticlephp method of traversing all elements contained in a class, php This article describes the example of php traversing all elements contained in a class method. Share it with everyone for your reference. The specific analysis is as follows: Here...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn