首页 > 代码库 > PHP 单例

PHP 单例

 1 <?php
 2 
 3 session_start();
 4 
 5 class CartTool {
 6     private static $ins = null;
 7     private $items =array();
 8     public $sign=0;
 9 
10     final protected function __construct(){
11         $this->sign=mt_rand(1,100000);
12     }
13 
14     final protected function __clone(){
15 
16     }
17 
18     protected static function getIns(){
19         if(!(self::$ins instanceof self)){
20             self::$ins = new self();
21         }
22         return self::$ins;
23     }
24 
25     public static function getCart(){
26         if(!isset($_SESSION[‘cart‘]) || !($_SESSION[‘cart‘] instanceof self)){
27             $_SESSION[‘cart‘]=self::getIns();
28 
29         }
30 
31         return $_SESSION[‘cart‘];
32     }
33 
34 
35 }
36 
37 
38 print_r(CartTool::getCart());
39 
40 ?>