|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2000-11-02 13:59 UTC] danjrod at terra dot 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 20 08:00:02 2025 UTC |
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.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.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.