factory_and_singleton_001.phpt 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. --TEST--
  2. ZE2 factory and singleton, test 1
  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. Destruct static
  83. NULL
  84. int(0)
  85. Done