bug20175.phpt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. --TEST--
  2. Bug #20175 (Static vars can't store ref to new instance)
  3. --INI--
  4. error_reporting=E_ALL
  5. --FILE--
  6. <?php
  7. print zend_version()."\n";
  8. /* Part 1:
  9. * Storing the result of a function in a static variable.
  10. * foo_global() increments global variable $foo_count whenever it is executed.
  11. * When foo_static() is called it checks for the static variable $foo_value
  12. * being initialized. In case initialisation is necessary foo_global() will be
  13. * called. Since that must happen only once the return value should be equal.
  14. */
  15. $foo_count = 0;
  16. function foo_global() {
  17. global $foo_count;
  18. echo "foo_global()\n";
  19. return 'foo:' . ++$foo_count;
  20. }
  21. function foo_static() {
  22. static $foo_value;
  23. echo "foo_static()\n";
  24. if (!isset($foo_value)) {
  25. $foo_value = foo_global();
  26. }
  27. return $foo_value;
  28. }
  29. /* Part 2:
  30. * Storing a reference to the result of a function in a static variable.
  31. * Same as Part 1 but:
  32. * The return statement transports a copy of the value to return. In other
  33. * words the return value of bar_global() is a temporary variable only valid
  34. * after the function call bar_global() is done in current local scope.
  35. */
  36. $bar_count = 0;
  37. function bar_global() {
  38. global $bar_count;
  39. echo "bar_global()\n";
  40. return 'bar:' . ++$bar_count;
  41. }
  42. function bar_static() {
  43. static $bar_value;
  44. echo "bar_static()\n";
  45. if (!isset($bar_value)) {
  46. $bar_value = &bar_global();
  47. }
  48. return $bar_value;
  49. }
  50. /* Part 3: TO BE DISCUSSED
  51. *
  52. * Storing a reference to the result of a function in a static variable.
  53. * Same as Part 2 but wow_global() returns a reference so $wow_value
  54. * should store a reference to $wow_global. Therefore $wow_value is already
  55. * initialized in second call to wow_static() and hence shouldn't call
  56. * wow_global() again.
  57. */ /*
  58. $wow_count = 0;
  59. $wow_name = '';
  60. function &wow_global() {
  61. global $wow_count, $wow_name;
  62. echo "wow_global()\n";
  63. $wow_name = 'wow:' . ++$wow_count;
  64. return $wow_name;
  65. }
  66. function wow_static() {
  67. static $wow_value;
  68. echo "wow_static()\n";
  69. if (!isset($wow_value)) {
  70. $wow_value = &wow_global();
  71. }
  72. return $wow_value;
  73. }*/
  74. /* Part 4:
  75. * Storing a reference to a new instance (that's where the name of the test
  76. * comes from). First there is the global counter $oop_global again which
  77. * counts the calls to the constructor of oop_class and hence counts the
  78. * creation of oop_class instances.
  79. * The class oop_test uses a static reference to a oop_class instance.
  80. * When another oop_test instance is created it must reuse the statically
  81. * stored reference oop_value. This way oop_class gets some singleton behavior
  82. * since it will be created only once for all instances of oop_test.
  83. */
  84. $oop_global = 0;
  85. class oop_class {
  86. var $oop_name;
  87. function __construct() {
  88. global $oop_global;
  89. echo "oop_class()\n";
  90. $this->oop_name = 'oop:' . ++$oop_global;
  91. }
  92. }
  93. class oop_test {
  94. static $oop_value;
  95. function __construct() {
  96. echo "oop_test()\n";
  97. }
  98. function oop_static() {
  99. echo "oop_static()\n";
  100. if (!isset(self::$oop_value)) {
  101. self::$oop_value = new oop_class;
  102. }
  103. echo self::$oop_value->oop_name;
  104. }
  105. }
  106. print foo_static()."\n";
  107. print foo_static()."\n";
  108. print bar_static()."\n";
  109. print bar_static()."\n";
  110. //print wow_static()."\n";
  111. //print wow_static()."\n";
  112. echo "wow_static()
  113. wow_global()
  114. wow:1
  115. wow_static()
  116. wow:1
  117. ";
  118. $oop_tester = new oop_test;
  119. print $oop_tester->oop_static()."\n";
  120. print $oop_tester->oop_static()."\n";
  121. $oop_tester = new oop_test; // repeated.
  122. print $oop_tester->oop_static()."\n";
  123. ?>
  124. --EXPECTF--
  125. %s
  126. foo_static()
  127. foo_global()
  128. foo:1
  129. foo_static()
  130. foo:1
  131. bar_static()
  132. bar_global()
  133. Notice: Only variables should be assigned by reference in %sbug20175.php on line 47
  134. bar:1
  135. bar_static()
  136. bar:1
  137. wow_static()
  138. wow_global()
  139. wow:1
  140. wow_static()
  141. wow:1
  142. oop_test()
  143. oop_static()
  144. oop_class()
  145. oop:1
  146. oop_static()
  147. oop:1
  148. oop_test()
  149. oop_static()
  150. oop:1