factory_and_singleton_002.phpt 1.5 KB

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