TP中使用单例不生效

浏览:1043 发布日期:2014/10/10 分类:求助交流 关键字: 单例,session
我写好的单例放在ThinkPHP/Extend/Library/COM/JG/CartTool.class.php中,在Action中使用import('COM.JG.CartTool')引入。使用过程中单利始终不能生效。
CartTool.class.php代码如下:class CartTool extends Think{
    private static $ins = null;
    private $items = array();
    public $sing = 0;
    
    protected function __construct(){
        $this->sing = mt_rand(1, 20000);
    }
    
    public static function get_ins(){
        if(!(self::$ins instanceof self)){
            self::$ins = new self();
        }
        
        return self::$ins;
    }
    
    public static function get_cart(){
        if(!isset($_SESSION['cart']) || !($_SESSION['cart'] instanceof self)){
            $_SESSION['cart'] = self::get_ins();
        }
        
        return $_SESSION['cart'];
    } 
    
}
最佳答案
评论() 相关
后面还有条评论,点击查看>>