当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP中的reflection反射机制测试例子

PHP中的reflection反射机制测试例子

2019年03月20日  | 移动技术网IT编程  | 我要评论

java类反射应用得非常广泛几乎是所有框架的最核心部分,php程序员似乎从不关心反射。尝试着用java的思想去理解php的反射,跟java基本上基本一致。参考了php手册:http://www.php.net/manual/zh/book.reflection.php。

reflecttest.php:

<?php
 
class reflecttest {
 
    /**
     * 用户id
     */
    private $userid;
 
    /**
     * 用户名
     */
    private $username;
 
    /**
     * 用户密码
     */
    private $password;
 
    /**
     * 用户邮箱
     */
    private $email;
 
    /**
     * 用户qq号码
     */
    private $qq;
 
    /**
     * 登陆次数
     */
    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->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");//实例化reflecttest
  echo "<h1>reflecttest init.</h1><br/>userid:".$ref->getuserid()."<br/>username:".$ref->getusername()."<br/>password:".$ref->getpassword();
  $class = new reflectionclass('reflecttest');//反射加载reflecttest类
  $instance = $class->newinstanceargs(array('123','root','123456'));//reflecttest初始化
 
  echo "<h1>field:</h1><br/>";
  $field = $class->getproperties();
  foreach($field as $f) {
    echo $f->getname()."<br/>";//反射输出所有的成员变量
  }
 
  echo "<h1>get fields doccomment:</h1><br/>";
  foreach($field as $f) {
    $doccomment = $f->getdoccomment();//反射输出所有成员变量的文档注释
    echo $doccomment."<br/>";
  }
 
  $method = $class->getmethods();//获取reflecttest所有方法
  echo "<h1>get methods doccomment:</h1><br/>";
  foreach($method as $m) {
    $doccomment = $m->getdoccomment();//获取所有方法的文档注释
    echo $doccomment."<br/>";
 
  }
 
  echo "<h1>get methods:</h1><br/>";
  foreach($method as $m) {
    $k = "get";//只调reflecttest中的所有的get方法
    echo $m->getname()."=".($k === "" || strpos ( $m->getname (), $k ) === 0?$m->invoke($instance):"")."<br/>";
    if("setqq"==$m->getname()){
      $m->invoke($instance,'441637262');//调用setqq方法为reflecttest当中的成员变量qq设值
    }
  }
 
  echo "<h1>invoke (set/get)qq result:</h1><br/>";
  $qq=$class->getmethod('getqq');//获取getqq方法
  echo "getqq:".$qq->invoke($instance)."<br/>";//获取成员变量qq的值
  echo "jb51.net";
?>

请求http://localhost/php/test/test.php输出结果:

reflecttest init.
 
userid:1
username:admin
password:admin888
field:
 
userid
username
password
email
qq
logintimes
get fields doccomment:
 
/** * 用户id */
/** * 用户名 */
/** * 用户密码 */
/** * 用户邮箱 */
/** * 用户qq号码 */
/** * 登陆次数 */
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=root
getpassword=123456
getemail=
getqq=
getlogintimes=
setuserid=
setusername=
setpassword=
setemail=
setqq=
setlogintimes=
invoke (set/get)qq result:
 
getqq:441637262
jb51.net

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网