php類比實現依賴注入,為什麼注釋的代碼會報錯,想了很久加百度過還是不懂

來源:互聯網
上載者:User
詳細代碼如下:
//類比實現依賴注入
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); 要看的是這個值。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.