The reflection reflection mechanism test example in PHP, phpreflection
Java class reflection is applied very broadly to almost all of the core parts of the framework, and PHP programmers seem to never care about reflection. Trying to understand PHP's reflection in Java is basically consistent with Java. Refer to the PHP Manual: http://www.php.net/manual/zh/book.reflection.php.
reflecttest.php:
<?php class Reflecttest {/** * User ID */private $userId; /** * User name */private $userName; /** * User Password */private $password; /** * User Mailbox */private $email; /** * User QQ number */private $qq; /** * Number of landing times */private $loginTimes; Public Function Reflecttest () {} public Function __construct ($userId, $userName, $password) {$this->userid = $userId; $this->username = $userName; $this->password = $password; }/** * * @return The $USERID * * Public Function getUserId () {return $this->userid; }/** * * @return The $userName * * Public Function GetUserName () {return $this->username; }/** * * @return The $password * * Public Function GetPassword () {return $this->password; }/** * * @return The $email * * Public Function Getemail () {return $this->email; }/** * * @return The $QQ * * Public Function getqq () {return $this->qq; }/** * * @return The $loginTimes * * Public Function Getlogintimes () {return $this->logintim Es }/** * * @param field_type $userId */Public Function Setuserid ($userId) {$this-&G T;userid = $userId; }/** * * @param field_type $userName */Public Function Setusername ($userName) {$thi S->username = $userName; }/** * * @param field_type $password */Public Function SetPassword ($password) {$thi S->password = $password; }/** * * @param field_type $email */Public Function Setemail ($email) {$this->emai L = $email; }/** * * @param field_type $QQ */Public Function setqq ($QQ) {$this->qq = $qq; }/** * * @param field_type $loginTiMES */Public Function setlogintimes ($loginTimes) {$this->logintimes = $loginTimes; }}?>
test.php:
<?php require_once ' reflecttest.php '; $ref = new Reflecttest ("1", "admin", "admin888");//Instantiate reflecttest echo "Reflecttest init.
UserId: ". $ref->getuserid ()."
UserName: ". $ref->getusername ()."
Password: ". $ref->getpassword (); $class = new Reflectionclass (' reflecttest ');//Reflection loading Reflecttest class $instance = $class->newinstanceargs (Array (' 123 ', ' Root ', ' 123456 ');//reflecttest initialize echo "Field:
"; $field = $class->getproperties (); foreach ($field as $f) {echo $f->getname (). "
";//reflection outputs all member variables} echo"Get fields DocComment:
"; foreach ($field as $f) {$docComment = $f->getdoccomment ();//Reflection outputs document comments for all member variables echo $docComment. "
"; } $method = $class->getmethods ();//Get reflecttest all methods echo "Get Methods DocComment:
"; foreach ($method as $m) {$docComment = $m->getdoccomment ();//Gets documentation comments for all methods echo $docComment. "
"; } echo "Get Methods:
"; foreach ($method as $m) {$k = "get";//Only reflecttest all of the Get methods in the Echo $m->getname (). " =". ($k = = = "" | | Strpos ($m->getname (), $k) = = = 0? $m->invoke ($instance): ""). "
"; if ("setqq" = = $m->getname ()) {$m->invoke ($instance, ' 441637262 ');//Call setqq method to set value for member variable QQ in Reflecttest}} Echo "Invoke (set/get) Qq Result:
"; $QQ = $class->getmethod (' getqq ');//Get the Getqq method echo "GETQQ:". $qq->invoke ($instance). "
";//Get member variable QQ value echo" Jb51.net ";? >
Request http://localhost/php/test/Test.php Output Result:
Reflecttest init. Userid:1username:adminpassword:admin888field:useridusernamepasswordemailqqlogintimesget fields DocComment:/** * User ID *//** * User name *//** * User Password *//** * User Email *//** * User QQ number *//** * Login */get Methods DocComment:/** * * @return the $user Id *//** * * @return The $userName *//** * * @return The $password *//** * * @return The $email *//** * * @return the $QQ *//** * * @return The $loginTimes *//** * * @param field_type $userId *//** * * @param field_type $userName *//** * * @par AM Field_type $password *//** * * @param field_type $email *//** * * @param field_type $qq *//** * * @param field_type $lo Gintimes */get methods:reflecttest=__construct=getuserid=123getusername=rootgetpassword=123456getemail=getqq= Getlogintimes=setuserid=setusername=setpassword=setemail=setqq=setlogintimes=invoke (set/get) Qq result:getqq : 441637262jb51.net
A detailed explanation of Java Reflection (Java reflection) mechanism
It's starting to pour out again, is it interesting?
What is the reflection mechanism of PHP
can also be called mappings. To be straightforward, not only can he clone objects, but the variables that can invoke the object are very
To the method, quite powerful. PHP API5 with an explanation of the object, there is a chance to look down, similar to
In Java. Of course, this feature is enough to prove that PHP and ASP still have a very big difference!
http://www.bkjia.com/PHPjc/858746.html www.bkjia.com true http://www.bkjia.com/PHPjc/858746.html techarticle PHP Reflection Reflection Mechanism Test example, Phpreflection Java class Reflection application is very broad almost the core of all frameworks, PHP programmers seem to never care about reflection. Try to ...