单元素模式

单元素模式

作者:LAMP小白  点击:1830  发布日期:2012-10-08 23:13:00  返回列表
所谓单元素就是提供对自身实例的共享,使他只会被实例化一次

简单实现代码就超简单了 只有3行

mio_lt;?php
    class inventoryConnection
    {
        protected static $_instance = null;
        protected $_handle = null;
        public static function getInstance()
        {
            //检查是否有实例化的自己 就是incentoyConnevtion存在
            //有就return 没有就new 自己 这样就保证了只有一个数据库连接存在
            if(!self::$_instance instanceof self)
                self::$_instance = new self;
            return self::$_instance;
        }
        protected function __construct()
        {
            $this-mio_gt;handle = 'mysql object';
        }
    }
?mio_gt;




上一篇:代理模式 下一篇:快递查询API
0