PHP reflection mechanism test example, phpreflection. The reflection mechanism test example in PHP. phpreflectionJava class reflection is widely used, and is the core of almost all frameworks. PHP programmers seem to never care about reflection. PHP reflection mechanism testing example, phpreflection
Java reflection is widely used, and is the core of almost all frameworks. PHP programmers do not seem to care about reflection. I tried to use java to understand php Reflection, which is basically the same as 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 email */private $ email;/***** User qq number */private $ qq;/***** login times */private $ loginTimes; public function ReflectTest () {} public function _ construct ($ userId, $ userName, $ password) {$ this-> userId = $ userId; $ this-> userName = $ userName; $ this-> password = $ password;}/***** @ re Turn 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 $ t His-> qq;}/***** @ return the $ loginTimes */public function getLoginTimes () {return $ this-> loginTimes ;} /***** @ param field_type $ userId */public function setUserId ($ userId) {$ this-> userId = $ userId ;} /***** @ param field_type $ userName */public function setUserName ($ userName) {$ this-> userName = $ userName ;} /***** @ param field_type $ password */public function setPassword ($ password ){ $ This-> password = $ password;}/***** @ param field_type $ email */public function setEmail ($ email) {$ this-> email = $ 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 loaded ReflectTest class $ instance = $ class-> newInstanceArgs (array ('20140901 ', 'root', '123'); // ReflectTest initializes 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 (); // The comments echo $ docComment ."
";}$ Method = $ class-> getMethods (); // Obtain all ReflectTest Methods echo" get Methods DocComment:
"; Foreach ($ method as $ m) {$ docComment = $ m-> getDocComment (); // obtain the document comment for all methods echo $ docComment ."
";} Echo" get Methods:
"; Foreach ($ method as $ m) {$ k =" get "; // only call all get methods in ReflectTest echo $ m-> getName (). "= ". ($ k = "" | strpos ($ m-> getName (), $ k) = 0? $ M-> invoke ($ instance ):"")."
"; If (" setQq "==$ m-> getName () {$ m-> invoke ($ instance, '20140901 '); // call the setQq method to set the value of the member variable qq in ReflectTest} echo "Invoke (set/get) Qq result:
"; $ Qq = $ class-> getmethod ('getqq'); // Obtain the getQq method echo" getQq: ". $ qq-> invoke ($ instance )."
"; // Obtain the qq value of the member variable echo" jb51.net ";?>
Request http: // localhost/php/test/Test. php output result:
ReflectTest init. userId: 1 UserName: adminPassword: admin888Field: userIduserNamepasswordemailqqloginTimesget Fields DocComment: /*** User ID */*** User name */*** user password */*** User email **/*** User QQ number *//* ** login times */get Methods DocComment: /***** @ return the $ userId * // ***** @ return the $ userName * // ***** @ return the $ password *//*** * @ return the $ email * // ***** @ return the $ qq * // ***** @ return the $ loginTimes * // ***** @ param field_type $ userId * // ***** @ param field_type $ userName * // ***** @ param field_type $ password * // ***** @ param field_type $ email */ /***** @ param field_type $ qq * // ***** @ param field_type $ loginTimes */get Methods: reflectTest =__ construct = getUserId = 123 getUserName = rootgetPassword = 123456 getEmail = getQq = getLoginTimes = setUserId = setUserName = setPassword = setEmail = setQq = setLoginTimes = Invoke (set/get) Qq result: getQQ: 441637262jb51.net
Java Reflection (JAVA Reflection) mechanism
I started to score again. is it interesting?
What is PHP Reflection mechanism?
It can also be called ING. To put it bluntly, he can not only clone objects, but also call object variables
The method is quite powerful. Php API5 has an explanation of the object and has the opportunity to read it, similar
In java. Of course, this feature is enough to prove that php and asp are quite different!
Runtime Java class reflection is widely used and is the core of almost all frameworks. PHP programmers do not seem to care about reflection. Trying...