class_dependency.xml
<?xml version="1.0" standalone="yes"?>
<dependency>
<gclass>
<mainclass>mainClassName</mainclass>
<property>
<propertyname>propertyName</propertyname>
<toolclass>toolClassName</toolclass>
</property>
</gclass>
<gclass>
<mainclass>Girl</mainclass>
<property>
<propertyname>Boy</propertyname>
<toolclass>Boy</toolclass>
</property>
<property>
<propertyname>Cat</propertyname>
<toolclass>Cat</toolclass>
</property>
</gclass>
</dependency>
<?php
/*
* Created on 2007-10-31
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse -
PHP - Code Templates
*/
interface IOCinterface{
function setProperty($key,$value);
function getProperty($key);
}
class Boy
{
public $Boy="Boy";
function kiss()
{
echo "Kiss boy ";
}
}
class Cat
{
public $Cat="Cat";
function kiss()
{
echo "Kiss cat ";
}
}
class Girl implements IOCinterface{
private $Boy;
private $Cat;
function setProperty($property,$class)
{
$this->$property=$class;
}
function getProperty($key)
{
return $this->$key;
}
function kissBoy()
{
$this->Boy->kiss();
}
function kissCat()
{
$this->Cat->kiss();
}
}
class DefaultPicoContainer
{
private $classArray = array();
function registerComponentImplementation($className)
{
if(array_key_exists($className,$this->classArray)){}
else
{
$dependencyArray=$this->getDependencyArray($className);
if(empty($dependencyArray))
{
$mainClass = new $className();
$this->classArray["$className"]=&$mainClass;
}
else
{
$mainClass = new $className();
while(list($property,$toolClass)=each($dependencyArray))
{
$temp=(string)$toolClass;
$tempClass = new $temp();
$setProperty="setProperty";
if(array_key_exists($temp,$this->classArray))
{
$mainClass->setProperty($temp,$this->classArray["$temp"]);
}
else
{
$this->classArray["$temp"]=&$tempClass;
//var_dump();
$mainClass->setProperty($temp,$this->classArray["$temp"]);
}
//echo $property.$toolClass;
}
$this->classArray["$className"]=&$mainClass;
}
}
}
function getComponentInstance($className)
{
if(array_key_exists($className,$this->classArray))
{
return $this->classArray[$className];
}
else
{
die("Class not exist.");
}
}
function getDependencyArray($mainClassName)
{
$dependencyArray="";
if (file_exists('class_dependency.xml'))
{
$xml = simplexml_load_file('class_dependency.xml');
$associateArray = get_object_vars($xml);
for($i=0,$maxi=count($associateArray['gclass']);$i<$maxi;$i++)
{
$gclassArray=$associateArray['gclass'][$i];
if($gclassArray->mainclass == $mainClassName)
{
$maxj=count($gclassArray->property);
if($maxj>1)
{
for($j=0;$j<$maxj;$j++)
{
$object=$gclassArray->property[$j];
$dependencyArray["$object->propertyname"]=$object->toolclass;
}
//var_dump($dependencyArray);
}
else
{
$object=$gclassArray->property;
$dependencyArray["$object->propertyname"]=$object->toolclass;
}
}
}
} else {
die('Failed to open test.xml.');
}
if(empty($dependencyArray))
{
return 0;
}
else
{
return $dependencyArray;
}
}
}
$container = new DefaultPicoContainer();
$container->registerComponentImplementation("Boy");
$container->registerComponentImplementation("Cat");
$container->registerComponentImplementation("Girl");
$girl = $container->getComponentInstance("Girl");
$girl->kissBoy();
$girl->kissCat();
?>
上面的例子虽然实现了依赖注入,但是由于php本身是一种弱类型的语言,当类型发生变化时并不会报错,从而失去了ioc的精髓,因此有其形而无其神。