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.