解析php中反射的應(yīng)用
<?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);//根據(jù)配置configData的名稱,實(shí)例化ReflectionClass
if(!$module_class->isSubclassOf($interface)){//檢查反射得到了類是否是$interface的子類
throw new Exception("unknown module type:$modulename");//不是Module子類則拋出異常
}
$module=$module_class->newInstance();//實(shí)例化一個(gè)FtpModule或者PersonModule對(duì)象
foreach($module_class->getMethods() as $method){//獲得類中的方法
$this->handleMethod($module,$method,$params);
}
array_push($this->modules,$module);//將實(shí)例化的module對(duì)象放入$modules數(shù)組中
}
}
function handleMethod(Module $module,ReflectionMethod $method,$params){
$name=$method->getName();//獲得方法名稱
$args=$method->getParameters();//獲得方法中的參數(shù)
if(count($args)!=1||substr($name,0,3)!="set"){//檢查方法必須是以set開(kāi)頭,且只有一個(gè)參數(shù)
return false;
}
$property=strtolower(substr($name,3));//講方法名去掉set三個(gè)字母,作為參數(shù)
if(!isset($params[$property])){//如果$params數(shù)組不包含某個(gè)屬性,就返回false
return false;
}
$arg_class=@$args[0]->getClass;//檢查setter方法的第一個(gè)參數(shù)(且唯一)的數(shù)據(jù)類型
if(empty($arg_class)){
$method->invoke($module,$params[$property]);
}else{
$method->invoke($module,$arg_class->newInstance($params[$property]));
}
}
}
$test=new ModuleRunner();
$test->init();
?>
二 通過(guò)反射獲得類中信息:
<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>
結(jié)果是: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>
- PHP的反射類ReflectionClass、ReflectionMethod使用實(shí)例
- PHP反射類ReflectionClass和ReflectionObject的使用方法
- PHP反射機(jī)制原理與用法詳解
- PHP反射使用實(shí)例和PHP反射API的中文說(shuō)明
- PHP反射機(jī)制用法實(shí)例
- 在PHP中使用反射技術(shù)的架構(gòu)插件使用說(shuō)明
- php反射類ReflectionClass用法分析
- PHP 反射(Reflection)使用實(shí)例
- PHP基于反射機(jī)制實(shí)現(xiàn)自動(dòng)依賴注入的方法詳解
- PHP中的reflection反射機(jī)制測(cè)試?yán)?/a>
- PHP進(jìn)階學(xué)習(xí)之反射基本概念與用法分析
相關(guān)文章
PHP獲取表單數(shù)據(jù)與HTML嵌入PHP腳本的實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇PHP獲取表單數(shù)據(jù)與HTML嵌入PHP腳本的實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02詳談php中 strtr 和 str_replace 的效率問(wèn)題
下面小編就為大家?guī)?lái)一篇詳談php中 strtr 和 str_replace 的效率問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05在PHP程序中運(yùn)行Python腳本(接收數(shù)據(jù)及傳參)的方法詳解
這篇文章主要為大家詳細(xì)介紹一下,如何在php程序中運(yùn)行Python腳本以及如何使用python返回josn數(shù)據(jù)供php使用,感興趣的小伙伴可以了解一下2022-09-09php對(duì)二維數(shù)組進(jìn)行相關(guān)操作(排序、轉(zhuǎn)換、去空白等)
這篇文章主要介紹了php對(duì)二維數(shù)組進(jìn)行相關(guān)操作,包括php對(duì)二維數(shù)組排序、轉(zhuǎn)換、去空白,以及去重復(fù)值等,感興趣的小伙伴們可以參考一下2015-11-11php安全配置記錄和常見(jiàn)錯(cuò)誤梳理(總結(jié))
下面小編就為大家?guī)?lái)一篇php安全配置記錄和常見(jiàn)錯(cuò)誤梳理(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03php可應(yīng)用于面包屑導(dǎo)航的迭代尋找家譜樹(shù)實(shí)現(xiàn)方法
這篇文章主要介紹了php可應(yīng)用于面包屑導(dǎo)航的迭代尋找家譜樹(shù)實(shí)現(xiàn)方法,涉及php迭代的技巧與應(yīng)用方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02PHP+Mysql基于事務(wù)處理實(shí)現(xiàn)轉(zhuǎn)賬功能的方法
這篇文章主要介紹了PHP+Mysql基于事務(wù)處理實(shí)現(xiàn)轉(zhuǎn)賬功能的方法,實(shí)例分析了mysql事務(wù)處理的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07