How to use PHP reflection classes ReflectionClass and ReflectionObject_PHP tutorial

WBOY
Release: 2016-07-13 10:25:09
Original
1353 people have browsed it

The extended reflection class in PHP is used to analyze PHP programs and export or extract detailed information about classes, methods, properties, parameters, etc., including comments.
Look at a question like this. The member variables of the PHP class are not declared in the class, but are declared in the function. What is the difference?

Copy code The code is as follows:

class test{
private $name;
private $sex;
function __construct(){
$this->aaa='aaa';
}
}

$test=new test();

$ reflect=new ReflectionClass($test);
$pro=$reflect->getDefaultProperties();
print_r($pro);//Print result: Array ( [name] => [sex] = > )

echo $test->aaa;//Print result: aaa

In this test class, two member variables $name and $sex are declared, However, in the constructor, another variable $aaa is declared, the class is initialized, and the reflection class is used to print the default member attributes. Only the two declared member variable attributes are found, but the $aaa variable of the printing class is still able to output the result.
Excuse me, it is not necessary to declare the member variables of the class. Is it possible to declare them in the function? What is the difference?
In your example, using ReflectionClass is inappropriate because __construct will only be executed when the class is instantiated.
That is to say, ReflectionClass is more of the structure when the reflection class is declared, rather than the structure after the class is instantiated, so it is correct that the attribute aaa is not output, because the attribute aaa does not exist (when the class is declared).
So how do you view the attribute aaa? You should use ReflectionObject to reflect the instantiated structure, such as
Copy code The code is as follows:

class test{
private $name;
private $sex;
function __construct(){
$this->aaa='aaa';
}
}
$test=new test();

$reflect=new ReflectionObject($test);
$pro=$reflect->getProperties();
print_r($pro);

The attribute aaa will exist only after instantiation, and then you can see the attribute aaa
Because PHP is a "dynamic" language, so The member variables of the class do not need to be declared, and they can be declared in the function.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825185.htmlTechArticleExtended reflection class in PHP. This extension is used to analyze PHP programs and export or extract information about classes, methods, Details of properties, parameters, etc., including comments. Look at a question like this, php class...
Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!