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