1--TEST-- 2ZE2 factory and singleton, test 2 3--FILE-- 4<?php 5class test { 6 protected $x; 7 8 static private $test = NULL; 9 static private $cnt = 0; 10 11 static function factory($x) { 12 if (test::$test) { 13 return test::$test; 14 } else { 15 test::$test = new test($x); 16 return test::$test; 17 } 18 } 19 20 protected function __construct($x) { 21 test::$cnt++; 22 $this->x = $x; 23 } 24 25 static function destroy() { 26 test::$test = NULL; 27 } 28 29 protected function __destruct() { 30 test::$cnt--; 31 } 32 33 public function get() { 34 return $this->x; 35 } 36 37 static public function getX() { 38 if (test::$test) { 39 return test::$test->x; 40 } else { 41 return NULL; 42 } 43 } 44 45 static public function count() { 46 return test::$cnt; 47 } 48} 49 50echo "Access static members\n"; 51var_dump(test::getX()); 52var_dump(test::count()); 53 54echo "Create x and y\n"; 55$x = test::factory(1); 56$y = test::factory(2); 57var_dump(test::getX()); 58var_dump(test::count()); 59var_dump($x->get()); 60var_dump($y->get()); 61 62echo "Destruct x\n"; 63$x = NULL; 64var_dump(test::getX()); 65var_dump(test::count()); 66var_dump($y->get()); 67 68echo "Destruct y\n"; 69$y = NULL; 70var_dump(test::getX()); 71var_dump(test::count()); 72 73//echo "Destruct static\n"; 74//test::destroy(); 75//var_dump(test::getX()); 76//var_dump(test::count()); 77 78echo "Done\n"; 79?> 80--EXPECT-- 81Access static members 82NULL 83int(0) 84Create x and y 85int(1) 86int(1) 87int(1) 88int(1) 89Destruct x 90int(1) 91int(1) 92int(1) 93Destruct y 94int(1) 95int(1) 96Done 97 98Warning: Call to protected test::__destruct() from global scope during shutdown ignored in Unknown on line 0 99