Php simulates dependency injection. Why does the annotated code report an error? I have been thinking about Baidu for a long time, but I still don't understand it.

Source: Internet
Author: User
Php simulates dependency injection. Why does the annotated code report an error? after thinking about adding Baidu for a long time, I still don't understand the detailed code as follows:
// Simulate dependency injection
Header ("content-type: text/html; charset = utf-8 ");
Interface Car {
Public function getBrand ();
Public function run ();
}
Class BMWCar implements Car {
Private $ myBrand = "BMW ";
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 = 'properties '){
$ 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]; // The two statements have different results. The comments report an error. why?
$ 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 ();
?>

Corresponds to the content in the xml file









I always think it is hard to understand. I hope someone can help me, and I think this problem is worth exploring.


Reply to discussion (solution)

When an error is reported, you can start to find the value of $ bean.

I know, I have also used echo $ bean; to detect it before, and then the browser displays c, corresponding to the id in the bean container. then I always thought there was no error, but echo gettype ($ bean); found that the output is an Object, but in fact the Object cannot be used as the array key, so the error is wrong here

In fact, you already have var_dump ($ this-> beans );
Why not look at his results?

$ PropertyBean = $ this-> beans [$ bean];
Change
$ PropertyBean = $ this-> beans [$ this-> bean];

$ PropertyBean = $ this-> beans [$ bean];
Change
$ PropertyBean = $ this-> beans [$ this-> bean];



In fact, you don't need to change it. Instead, an error is returned, that is, $ bean is a class object, $ this-> beans is an array, and the object cannot be used as an array key value.

In fact, you already have var_dump ($ this-> beans );
Why not look at his results?



Of course I have read it because the returned result is correct. I have also tried to test echo $ bean, and the output is bean. the corresponding id in xml, but in fact $ bean is an object. at the beginning, I should strictly use echo gettype ($ bean)

Well, yes. It's useless.
Check var_dump ($ this-> beans );
$ PropertyBean = $ this-> beans [$ bean];

If you are var_dump ($ bean); ($ this-> beans are custom attributes and you do not need to check validity at all)
You will see such a report.
Object (SimpleXMLElement) #7 (1 ){
[0] => string (1) "c"
}

$ PropertyBean = $ this-> beans ["$ bean"]; the normal reason is that it defines the _ toString method.

The name is too similar and prone to errors.

Var_dump ($ this-> beans); is an array

Var_dump ($ beans); it depends on this value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.