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