How to use PHP reflective class Reflectionclass and Reflectionobject _php tips

Source: Internet
Author: User
Tags php class reflection
An extended reflection class in PHP that is used to analyze PHP programs, to export or extract details about classes, methods, properties, parameters, and so on, including comments.
To see a problem like this, the member variables of the PHP class are not declared in the class, but are declared in the function, what's the difference?
Copy Code code 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, but in the constructor, a variable $aaa is declared, the class is initialized, the default member property is printed with a reflection class, and only the two member variable properties are declared, but the $AAA variable of the print class is found to be able to output the result.
The class member variable does not declare, in the function declaration is also OK, what is the difference?
In your case, it is not appropriate to use Reflectionclass because the __construct is executed only when the class is instantiated.
That is, Reflectionclass is more of a reflection class declaration structure, rather than a class-instantiated structure, so no output attribute AAA is correct, because the attribute AAA is indeed (when the class declaration) does not exist.
So how do you look at attribute AAA, the structure that should be instantiated with reflectionobject reflection, for example
Copy Code code as follows:

<?php
Class test{
Private $name;
Private $sex;
function __construct () {
$this->aaa= ' AAA ';
}
}
$test =new test ();

$reflect =new Reflectionobject ($test);
$pro = $reflect->getproperties ();
Print_r ($pro);

Once instantiated, the attribute AAA will exist, and you will be able to see the attribute AAA.
Because PHP is a "dynamic" language, it is possible to declare a member variable of a class without declaring it in a function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.