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

WBOY
Release: 2016-07-13 09:54:05
Original
1029 people have browsed it

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";
}
Copy after login

The running results are as follows:

TESTSTR1 => hello
TESTVAR1 => 1001
TESTVAR2 => 1002
Copy after login

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...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template