PHP simulation to implement dependency injection, why the code of comments will be error, think for a long time to spend or do not understand

Source: Internet
Author: User
The detailed code is as follows:
Simulation for 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 $key 1 = $value 1) {
if ($key 1== ' property ') {
$propertyName = $value 1[' name '];
$bean = $value 1[' bean ');
if (!isset ($value 1[' 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]; These two kinds of writing results are actually different, the comment inside will 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 ();
?>

corresponding to the content in the XML file









I always find it hard to understand, I hope it can help, and I think this problem is worth in-depth


Reply to discussion (solution)

When the error is, $bean value, you can start from here to find the problem.

Know, before also used Echo $bean, to detect, and then the browser display C, corresponding to the ID of the bean container, and then I always thought that there is no mistake, but just echo GetType ($bean), found that the output is an object, In fact, the object is not a key to the array, so it's wrong to be wrong here.

In fact you already have Var_dump ($this->beans);
Why don't you take a look at his results?

$propertyBean = $this->beans[$bean];
Switch
$propertyBean = $this->beans[$this->bean];

$propertyBean = $this->beans[$bean];
Switch
$propertyBean = $this->beans[$this->bean];



In fact, do not change, change the error, that is, this $bean is a class object, $this->beans is a group, the object cannot be used as an array key value

In fact you already have Var_dump ($this->beans);
Why don't you take a look at his results?



Of course I looked, precisely because the return is the right thing, I also tried to test echo $bean; the output is also the bean.xml inside the corresponding ID, but in fact $bean is an object, I should be strict with Echo GetType ($bean)

Well, yes. It's useless to see it.
Looking at Var_dump ($this->beans);
Using $propertyBean = $this->beans[$bean];

If you are Var_dump ($bean); ($this->beans is a property of its own definition and does not need to be checked for validity at all)
Then you'll see a report like this.
Object (SimpleXMLElement) #7 (1) {
[0]=> string (1) "C"
}

$propertyBean = $this->beans["$bean"]; The normal reason is because he defines the __tostring method.

The name is too similar to error-prone.

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

Var_dump ($beans); This is the value to see.

  • 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.