php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #7598 modify "new" syntax
Submitted: 2000-11-02 13:59 UTC Modified: 2000-12-05 13:45 UTC
From: danjrod at terra dot es Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.0.3pl1 OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: danjrod at terra dot es
New email:
PHP Version: OS:

 

 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-11-02 14:15 UTC] waldschrott@php.net
how did you verify/find out that it is copied?
 [2000-11-02 15:12 UTC] danjrod at terra dot 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:39 UTC] danjrod at terra dot 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:43 UTC] danjrod at terra dot 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-12-05 13:45 UTC] andi@php.net
Can you please check out the latest CVS of php4/Zend/TSRM and let me know if it works with you when using the:
$var =& new foo() syntax.
Or you can wait a day to get the latest snapshot of the PHP development tree
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Dec 04 22:05:52 2022 UTC