PHP 4.0 Bug #7598 Updated: modify "new" syntax

From: Date: Tue, 21 Nov 2000 09:01:37 +0000
Subject: PHP 4.0 Bug #7598 Updated: modify "new" syntax
Groups: php.dev 
Request: Send a blank email to php-dev+get-38763@lists.php.net to get a copy of this message
ID: 7598 Updated by: sniper Reported By: danjrod@terra.es Status: Open Bug Type: Feature/Change Request Assigned To: Comments: Previous Comments: --------------------------------------------------------------------------- [2000-11-02 15:39:53] danjrod@terra.es With a simple script like the one below .... Having a look at zend-parser.y also gives an insight into the problem. I can provide an adequate patch for both of the alternatives I proposed in the "feature/change" request. Hope this helps. I am typing the script in my portable, and thus it may contain a typo - I have checked twice before submitting :-) - but I hope it doesn't. Regards Daniel ---- script ---- class t1_t { var $value; function t1_t( &$p, $value) { $this->value = $value; $p->add( $this); } function output() { echo "<br>" . $this->value; } } class t_t { var $child; function t_t() { } function add( &$child) { $this->child = &$child; } function output() { $this->child->output(); } } $t = new t_t(); $t1 = new t1_t( $t, 5); $t1->value = 8; $t->output(); // output should be // <br>8 // but the real output is // <br>5 // since $t has stored the object during construction // and $t1 the copy instead of the reference. --------------------------------------------------------------------------- [2000-11-02 15:12:56] danjrod@terra.es With a simple script like the one below .... Having a look at zend-parser.y also gives an insight into the problem. I can provide an adequate patch for both of the alternatives I proposed in the "feature/change" request. Hope this helps. I am typing the script in my portable, and thus it may contain a typo - I have checked twice before submitting :-) - but I hope it doesn't. Regards Daniel ---- script ---- class t1_t { var $value; function t1_t( &$p, $value) { $this->value = $value; $p->add( $this); } function output() { echo "<br>" . $this->value; } } class t_t { var $child; function t_t() { } function add( &$child) { $this->child = &$child; } function output() { $this->child->output(); } } $t = new t_t(); $t1 = new t1_t( $t, 5); $t1->value = 8; $t->output(); // output should be // <br>8 // but the real output is // <br>5 // since $t has stored the object during construction // and $t1 the copy instead of the reference. --------------------------------------------------------------------------- [2000-11-02 14:15:57] waldschrott@php.net how did you verify/find out that it is copied? --------------------------------------------------------------------------- [2000-11-02 13:59:11] danjrod@terra.es Assigning the value returned by "new xxxx()" to a variable creates a copy of the constructed object. "new" does not support returning references by default, or prepending an ampersand, '&', to have a reference returned. Thus the request is: - Modify new syntax to either: a) It always returns a reference. This is unlikely to break any existing code. At most people will be creating a reference to a reference, which won´t harm b) Support the '&' syntax like in "&new xxx", so that a reference can be asked for. This will not break any existing code. This only implies slightly modifying "zend-parser.y", and no further changes to any part of the zend engine. This change will cure the use of "$this" as a reference inside the object constructor, which has been wrongly dismissed as a "circular reference problem". Regards Daniel --------------------------------------------------------------------------- Full Bug description available at: http://bugs.php.net/?id=7598

« previous php.dev (#38763) next »