养浩然之气,活着不是为技术,请关心身边的人.

php中ioc

上一篇 / 下一篇  2007-11-01 10:18:12 / 个人分类:php

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的精髓,因此有其形而无其神。

TAG: php

引用 删除 Guest   /   2008-05-25 22:41:24
http://www.meinv880.cn 我的美女大小姐
http://column.iresearch.cn/u/fdytxz/ 小冯的专栏
http://www.xingchenbian33.cn 坏蛋是怎样炼成的
http://blog.csdn.net/fdy1txz/ 小说社区
http://www.jipinjiading36.cn   极品家丁
http://column.iresearch.cn/u/fdy2txz/ 心情驿站
http://blog.csdn.net/fdy2txz/ 流动的小说站
http://www.5pr5.cn 极品公子

http://blog.sina.com.cn/fdy1txz 我的美女大小姐
http://blog.sina.com.cn/fdy2txz 极品家丁
http://blog.sina.com.cn/fdy3txz 坏蛋是怎样炼成的
http://blog.sina.com.cn/fdy4txz 极品公子

产生链接如下:百度收藏
http://cang.baidu.com/fdy1txz 收藏 我的美女大小姐
http://cang.baidu.com/fdy2txz 收藏 极品家丁
http://cang.baidu.com/fdy3txz 收藏 坏蛋是怎样炼成的
http://cang.baidu.com/fdy4txz 收藏 极品公子
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

数据统计

  • 访问量: 21713
  • 日志数: 146
  • 建立时间: 2007-08-01
  • 更新时间: 2008-02-20

RSS订阅

Open Toolbar