Home > Backend Development > PHP Tutorial > PHP method to traverse all elements contained in a class_PHP tutorial

PHP method to traverse all elements contained in a class_PHP tutorial

WBOY
Release: 2016-07-13 09:53:38
Original
921 people have browsed it

php method of traversing all elements contained in a class

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

 ?

1

2

3

4

5

6

7

8

9

10

11

12

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

1

2

3

TESTSTR1 => hello

TESTVAR1 => 1001

TESTVAR2 => 1002

4

5

6

8 9 10 11 12
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"; }
The running results are as follows:  ?
1 2 3 TESTSTR1 => hello TESTVAR1 => 1001 TESTVAR2 => 1002
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/1000108.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000108.htmlTechArticlephp method of traversing all elements contained in a class 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: Available 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