bug20175.phpt 4.0 KB

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