程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 解析php中反射的應用

解析php中反射的應用

編輯:關於PHP編程

一  反射的使用:
復制代碼 代碼如下:
<?php
class Person{
 public $name;
 function __construct($name){
  $this->name=$name;
 }
}
interface Module{
 function execute();
}
class FtpModule implements Module{
 function setHost($host){
  print "FtpModule::setHost():$host\n";
 }
 function setUser($user){
  print "FtpModule::setUser():$user\n";
 }
 function execute(){
  //something
 }
}
class PersonModule implements Module{
 function setPerson(Person $person){
  print "PersonModule::setPerson:{$person->name}\n";
 }
 function execute(){
  //something
 }
}
class ModuleRunner{
 private $configData
        =array(
          "PersonModule"=>array('person'=>'bob'),
          "FtpModule"=>array('host'=>'example.com','user'=>'anon')
        );
 private $modules=array();
 function init(){
  $interface=new ReflectionClass('Module');
  foreach($this->configData as $modulename=>$params){
   $module_class=new ReflectionClass($modulename);//根據配置configData的名稱,實例化ReflectionClass
   if(!$module_class->isSubclassOf($interface)){//檢查反射得到了類是否是$interface的子類
    throw new Exception("unknown module type:$modulename");//不是Module子類則拋出異常
   }
   $module=$module_class->newInstance();//實例化一個FtpModule或者PersonModule對象
   foreach($module_class->getMethods() as $method){//獲得類中的方法
    $this->handleMethod($module,$method,$params);
   }
   array_push($this->modules,$module);//將實例化的module對象放入$modules數組中
  }
 }
 function handleMethod(Module $module,ReflectionMethod $method,$params){
  $name=$method->getName();//獲得方法名稱
  $args=$method->getParameters();//獲得方法中的參數
  if(count($args)!=1||substr($name,0,3)!="set"){//檢查方法必須是以set開頭,且只有一個參數
   return false;
  }
  $property=strtolower(substr($name,3));//講方法名去掉set三個字母,作為參數
  if(!isset($params[$property])){//如果$params數組不包含某個屬性,就返回false
   return false;
  }
  $arg_class=@$args[0]->getClass;//檢查setter方法的第一個參數(且唯一)的數據類型
  if(empty($arg_class)){
   $method->invoke($module,$params[$property]);
  }else{
   $method->invoke($module,$arg_class->newInstance($params[$property]));
  }
 }
}
$test=new ModuleRunner();
$test->init();
?>

二  通過反射獲得類中信息:
復制代碼 代碼如下:
<PRE class=php name="code"><?php
class ReflectionUtil{
 static function getClassSource(ReflectionClass $class){
  $path=$class->getFileName();
  $lines=@file($path);
  $from=$class->getStartLine();
  $to=$class->getEndLine();
  $len=$to-$from+1;
  return implode(array_slice($lines,$from-1,$len));
 }
}
$classname="Person";
$path="../practice/{$classname}.php";
if(!file_exists($path)){
  throw new Exception("No such file as {$path}");
}
require_once($path);
if(!class_exists($classname)){
 throw new Exception("No such class as {$classname}");
}
print ReflectionUtil::getClassSource(new ReflectionClass('Person'));
?>
</PRE><BR>
<PRE></PRE>
結果是:class Person{ public $age; public $name; function getName(){return "zjx";} function getAge(){return 12;} function __toString(){ $rs=$this->getName(); $rs.="(age".$this->getAge().")"; return $rs; } }
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved