PHP reflection mechanism test example _ php instance

Source: Internet
Author: User
This article mainly introduces the reflection mechanism test example in PHP. From this article, we can learn some reflection usage methods, for more information, see Java reflection, which 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" php.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: 441637262php.net

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.