[PHP-DEV] PHP 4.0 Bug #7598 Updated: modify "new" syntax From: andi <email protected>
Date: 12/05/00

ID: 7598
Updated by: andi
Reported By: danjrod <email protected>
Status: Closed
Bug Type: Feature/Change Request
Assigned To:
Comments:

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

Previous Comments:
---------------------------------------------------------------------------

[2000-11-02 15:43:03] danjrod <email protected>
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:53] danjrod <email protected>
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 <email protected>
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 <email protected>
how did you verify/find out that it is copied?

---------------------------------------------------------------------------

[2000-11-02 13:59:11] danjrod <email protected>
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

---------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online.

Full Bug description available at: http://bugs.php.net/?id=7598

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
For additional commands, e-mail: php-dev-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>