php模拟实现依赖注入,为什么注释的代码会报错,想了很久加百度过还是不懂

月影流觞 2016-05-03 07:59:50
详细代码如下:
<?php
//模拟实现依赖注入
header("content-type:text/html;charset=utf-8");
interface Car{
public function getBrand();
public function run();
}
class BMWCar implements Car{
private $myBrand="宝马";
public function getBrand(){
return $this->myBrand;
}
public function run(){
echo "<BR>".$this->myBrand." is running"."<BR>";
}
}
class Human{
private $car;
public function getCar(){
return $this->car;
}
public function setCar(Car $car){
$this->car=$car;
}
public function myCarRun(){
$this->car->run();
}
}

interface BeanFactory{
public function getBean($id);
}
class ApplicationContext implements BeanFactory{
private $beans=array();
public function __construct(){
$xmlstr=simplexml_load_file('beans.xml');
print_r($xmlstr);
echo "<BR>";
//$xml=new SimpleXMlElement('beans.xml',0,true);
foreach ($xmlstr->bean as $key => $value) {
$id=$value['id'];
$class=$value['class'];
$reflect=new ReflectionClass("$class");
$instance=$reflect->newInstanceArgs();

foreach ($value as $key1 => $value1) {
if($key1=='property'){
$propertyName=$value1['name'];
$bean=$value1['bean'];
if(!isset($value1['bean'])){
$methodName='set'.ucfirst($propertyName);
$method=$reflect->getMethod($methodName);
$method->invoke($instance);
}
else{
var_dump($this->beans);
$propertyBean=$this->beans["$bean"];
//$propertyBean=$this->beans[$bean]; //这两种写法结果居然不同,注释里面的会报错,为什么
$methodName='set'.ucfirst($propertyName);
echo $methodName;
$instance->$methodName($propertyBean);
}
}
}
$this->beans["$id"]=$instance;
}
}
public function getBean($id){
return $this->beans[$id];
}
}

$context=new ApplicationContext();
$car=$context->getBean('c');
$car->run();
$human=$context->getBean('human');
$human->myCarRun();
?>

对应xml文件里的内容
<beans>
<bean id="c" class="BMWCar">
</bean>
<bean id="human" class="Human">
<property name="car" bean="c"></property>
<x name="bus"></x>
</bean>
</beans>

总觉得很费解,希望有心人可以帮忙,而且我也觉得这个问题很值得深入
...全文
213 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2016-05-05
  • 打赏
  • 举报
回复
嗯,是的。看了也没用 查看的是 var_dump($this->beans); 使用的是 $propertyBean=$this->beans[$bean]; 如果你是 var_dump($bean); ($this->beans 是自己定义的属性,根本不需要检查有效性) 那么就会看到这样的报告 object(SimpleXMLElement)#7 (1) { [0]=> string(1) "c" } $propertyBean=$this->beans["$bean"]; 正常的原因是因为他定义了 __toString 方法
傲雪星枫 2016-05-05
  • 打赏
  • 举报
回复
名称用得太相似了容易出错。 var_dump($this->beans); 是数组 var_dump($beans); 要看的是这个值。
月影流觞 2016-05-04
  • 打赏
  • 举报
回复
引用 4 楼 fdipzone 的回复:
$propertyBean=$this->beans[$bean]; 改为 $propertyBean=$this->beans[$this->bean];
其实不用改的,改了反而报错,就是这个$bean是个类对象,$this->beans是个数组,对象不能作为数组键值
月影流觞 2016-05-04
  • 打赏
  • 举报
回复
引用 3 楼 xuzuning 的回复:
其实你已经有了 var_dump($this->beans); 为什么不看一下他的结果呢?
我当然看了,正是因为返回的是正确的东西,我也试过测试echo $bean;输出的也是bean.xml里面对应的id,但实际上$bean是个object对象,我当初应该严谨一点用echo gettype($bean)的
傲雪星枫 2016-05-04
  • 打赏
  • 举报
回复
$propertyBean=$this->beans[$bean]; 改为 $propertyBean=$this->beans[$this->bean];
xuzuning 2016-05-04
  • 打赏
  • 举报
回复
其实你已经有了 var_dump($this->beans); 为什么不看一下他的结果呢?
月影流觞 2016-05-03
  • 打赏
  • 举报
回复
知道了,之前也用过echo $bean;来检测, 然后浏览器显示的是c,对应bean容器里面的id,然后我一直以为没有错,但是刚echo gettype($bean);发现输出的是Object,然而事实上对象是不能作为数组的键的,所以错是错在这里
傲雪星枫 2016-05-03
  • 打赏
  • 举报
回复
报错的时候,$bean的值是什么,可以从这里入手找问题。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧