<?php
/**
现在我还不会使用XML文件的命名空间定义,所以暂时就是用java中spring的配置文件。请大家谅解。
我现在也是一个php的初学者。
由于每次写php程序的时候都要引入其他的php文件,这个让我很是头痛,
所以就模仿java中的spring的动态注入功能,
写了这么一段代码。这段代码我测试过还比较好用吧。
不过我认为还是用很多不合理的地方,和一些bug存在。
这个我会在以后慢慢修改。也请大家给与指点。
笔者:咋个办呢 QQ:285207813 date:2009-12-19
*/
/*
*
*
* 使用,将BeanFactory.php 和 application.xml 两个文件放在 项目的 根目录下。
*
* 类说明:
* Bean工厂 , 使用Bean工厂,必须先调用执行 init($filePath , $hostPath) 初始化方法。
* $filePath 配置 application.xml的路径(物理路径 或 绝对路径)
* $hostPath 服务器的根路径 默认 "/"
*
* 之后通过 getNewBeanByName($className) 方法获得想要的对象
* $className 为类的名字 也是 application.xml 中bean 的 id 值
* @author ai_zxc
*
*/
class BeanFactory {
private static $app ; // application.xml文件解析对象
private static $includeFile = false ;
/**
* @param String $filePath 配置文件路径
* @author ChenGang 2009-12-19 下午01:55:11
*/
public static function init($filePath , $hostPath){
if(!$hostPath)
$hostPath = "/" ;
/*获取application.xml解析后的对象*/
self::$app = new applicationXMLforObject($filePath) ;
/*动态引入类所在PHP文件地址*/
foreach (self::$app->getBeanConfigList("bean") as $o) {
$classPath = $o->getClass() ;
include_once($hostPath.$classPath);
}
/*引入成功 做 php 文件引入成功 标志*/
self::$includeFile = true ;
}
/**
* 获取想要的对象通过 指定 类的名字
* 想要从application.xml文件描述的类中获得想要的对象
*
* @package String $className 类的名字(bean的id值)
*/
public static function getNewBeanByName($className) {
if(!self::$includeFile)
return ;
/*遍历applicaion.xml配置文件,查看是否存在与$className匹配的id的值*/
if(!self::$app->getBeanConfig($className))
return ;
$refle = new ReflectionClass ($className);
$beanObj = self::newObj($refle) ;
/*通过setter方法注入需要的对象*/
$s = $refle->getMethods(); //拿到 所有的方法
for($i = 0 ; $i < count($s) ; $i++){
$tempMethodName = $s[$i]->getName() ; //得到 每一个 方法的 名字
if ($s[$i]->isPublic()) {
if(preg_match("/^(set)/" , $tempMethodName)){ //过滤出 需要的 setter方法
$tempBeanId = substr($tempMethodName , 3) ; //拿到需要的 id
$ro = new ReflectionClass ($beanObj);
$method = $ro->getMethod($tempMethodName) ;
$method->invoke ( $beanObj, self::getNewBeanByName($tempBeanId) );
}
}
}
/*通过setter方法注入需要的对象*/
return $beanObj ;
}
/**
* 创建对象 指定id 所对应的类的对象
* @param $refle $refle = new ReflectionClass ($className);
* @author ChenGang 2009-12-19 下午02:16:44
* @return BeanObj / false;
*/
private static function newObj($refle){
try {
if ($refle->isInstantiable ()) {
return $refle->newInstance ();
}
else{
return false ;
}
} catch (Exception $e) {
throw new Exception($e." 没有引入类文件!") ;
}
}
}
/**
* 执行指定类的指定方法
* @author ai_zxc
*/
class ClassOneDelegator {
private $targets;
function __construct($obj) {
$this->target [] = $obj;
}
function __call($name, $args) {
foreach ( $this->target as $obj ) {
$r = new ReflectionClass ( $obj );
if ($method = $r->getMethod ( $name )) {
if ($method->isPublic () && ! $method->isAbstract ()) {
return $method->invoke ( $obj, $args );
}
}
}
}
}
/**
* bean标签对应的实体
* @author ai_zxc
*
*/
class BeanConfig{
private $id ;
private $class ;
/**
* @return the $id
*/
public function getId() {
return $this->id;
}
/**
* @return the $class
*/
public function getClass() {
return $this->class;
}
/**
* @param $id the $id to set
*/
public function setId($id) {
$this->id = $id;
}
/**
* @param $class the $class to set
*/
public function setClass($class) {
$this->class = $class;
}
}
/**
* 解析指定的XML配置文件
* @author ai_zxc
*
*/
class applicationXMLforObject {
private $filePath ;
private $xml ;
private $beanConfig ;
private static $arr = null;
/**
* 构造函数
* @param $filePath 指定XML配置文件路径
* @author ChenGang 2009-12-19 下午01:46:59
*/
function __construct($filePath) {
if(file_exists($filePath)){
$this->filePath = $filePath ;
$this->xml = new DOMDocument();
$this->xml->load($filePath) ;
}
else{
$o = new ReflectionClass("applicationXMLforObject") ;
throw new Exception(" [ ".$o->getName()." 找不到要解析XML文件 ] ") ;
}
}
/**
* 通过配置文件中标签bean的属性id获得一个对应的实体bean
* @return the $beanConfig
*/
public function getBeanConfig($beanId) {
if(self::$arr) {
return self::$arr[$beanId] ;
}
self::$arr = $this->getBeanConfigList("bean");
return self::$arr[$beanId] ;
}
/**
* 获得所有bean标签的实体对象
* @param unknown_type $tag
* @author ChenGang 2009-12-19 下午01:48:42
*/
public function getBeanConfigList($tag){
// 获取所有的$tag标签
$tagDom = $this->xml->getElementsByTagName($tag);
$arr = array() ;
foreach($tagDom as $node){
$this->beanConfig = new BeanConfig() ;
if($node->attributes->item(0)->nodeName == "id" && $node->attributes->item(1)->nodeName == "class"){
$this->beanConfig->setId($node->attributes->item(0)->nodeValue) ;
$this->beanConfig->setClass($node->attributes->item(1)->nodeValue) ;
$arr[$node->attributes->item(0)->nodeValue] = $this->beanConfig ;
}
}
return $arr ;
}
}
?>
没有合适的资源?快使用搜索试试~ 我知道了~
PHP实现依赖注入-使用反射机制和动态代理技术
共28个文件
repository:6个
entries:6个
root:6个
5星 · 超过95%的资源 需积分: 50 36 下载量 181 浏览量
2009-12-26
21:16:47
上传
评论
收藏 9KB RAR 举报
温馨提示
现在我还不会使用XML文件的命名空间定义,所以暂时就是用java中spring的配置文件。请大家谅解。 我现在也是一个php的初学者。 由于每次写php程序的时候都要引入其他的php文件,这个让我很是头痛, 所以就模仿java中的spring的动态注入功能, 写了这么一段代码。这段代码我测试过还比较好用吧。 不过我认为还是用很多不合理的地方,和一些bug存在。 这个我会在以后慢慢修改。也请大家给与指点。 笔者:咋个办呢 QQ:285207813 date:2009-12-19
资源推荐
资源详情
资源评论
收起资源包目录
TestStringForPHP.v.1.0.rar (28个子文件)
TestStringForPHP
.project 526B
CVS
Entries 248B
Repository 18B
Root 39B
index.php 579B
.settings
org.eclipse.php.core.prefs 96B
BeanFactory.php 6KB
com
dao
CVS
Entries 45B
Repository 26B
Root 39B
UserDao.php 188B
CVS
Entries 52B
Repository 22B
Root 39B
action
CVS
Entries 49B
Repository 29B
Root 39B
loginAction.php 1KB
mode
CVS
Entries 56B
Repository 27B
Root 39B
Userclass_UserMode.php 567B
service
CVS
Entries 49B
Repository 30B
Root 39B
UserService.php 276B
applicationContext.xml 2KB
.buildpath 174B
共 28 条
- 1
资源评论
- wanbiaoke2018-12-13学习了反射机制
咋个办呢
- 粉丝: 41
- 资源: 30
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- measuring-performance.zip
- 基于Qt+C++实现的各种曲线+源码
- STC15系列库(带使用手册)
- 基于matlab车辆路径问题使用矩阵蚂蚁算法 车辆调度问题
- 较全的好几种调制解调方案的matlab代码,有ASK,BPSK,FSK,QAM等的源代码 对通信领域的研究人员很有帮助
- Python毕业设计-基于django的毕业设计管理系统(源码+数据库).zip
- Python毕业设计-基于django的毕业设计管理系统(源码+数据库).zip
- 用Pygame zero (pgzero)开发游戏(带例子)
- apache-tomcat-9.0.88.zip
- 基于Qt+C++实现的各种炫酷的样式表+源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功