factory_and_singleton_002.phpt 1.6 KB

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