详细代码如下:
//模拟实现依赖注入
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 "
".$this->myBrand." is running"."
";
}
}
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 "
";
//$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文件里的内容
总觉得很费解,希望有心人可以帮忙,而且我也觉得这个问题很值得深入
回复讨论(解决方案)
报错的时候,$bean的值是什么,可以从这里入手找问题。
知道了,之前也用过echo $bean;来检测, 然后浏览器显示的是c,对应bean容器里面的id,然后我一直以为没有错,但是刚echo gettype($bean);发现输出的是Object,然而事实上对象是不能作为数组的键的,所以错是错在这里
其实你已经有了 var_dump($this->beans);
为什么不看一下他的结果呢?
$propertyBean=$this->beans[$bean];
改为
$propertyBean=$this->beans[$this->bean];
$propertyBean=$this->beans[$bean];
改为
$propertyBean=$this->beans[$this->bean];
其实不用改的,改了反而报错,就是这个$bean是个类对象,$this->beans是个数组,对象不能作为数组键值
其实你已经有了 var_dump($this->beans);
为什么不看一下他的结果呢?
我当然看了,正是因为返回的是正确的东西,我也试过测试echo $bean;输出的也是bean.xml里面对应的id,但实际上$bean是个object对象,我当初应该严谨一点用echo gettype($bean)的
嗯,是的。看了也没用
查看的是 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 方法
名称用得太相似了容易出错。
var_dump($this->beans); 是数组
var_dump($beans); 要看的是这个值。

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

SublimeText3汉化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。