Bug #66200 [Opn->Nab]: the new operator DON'T returns a reference automatically

From: Date: Fri, 29 Nov 2013 13:25:27 +0000
Subject: Bug #66200 [Opn->Nab]: the new operator DON'T returns a reference automatically
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-183010@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66200&edit=1 ID: 66200 Updated by: requinix@php.net Reported by: erwan dot oger35 at gmail dot com Summary: the new operator DON'T returns a reference automatically -Status: Open +Status: Not a bug Type: Bug Package: Class/Object related PHP Version: 5.4.22 Block user comment: N Private report: N New Comment: Looks like the correct behavior to me: an object reference is not the same thing as a variable reference. A variable has data associated with the variable itself, and one of those pieces of data is what the value is. An object reference is where the "what the value is" is the same for multiple variables, however the "data associated with the variable itself" is different for each. A variable reference will have both variables be using the same "data associated with the variable itself" (and thus the same "what the value is"). Shortest and simplest explanation I can come up with right now. $a2 = &$a: $a and $a2 are essentially the same variable, and setting $a=null destroys the only copy of the A instance (also affecting $a2 along the way). The assignment did not break the $a/$a2 link but rather changed the value that the two share. $b versus $this: Two variables, both referring to the same object. However while they have the same value the two are not the same variable like $a and $a2 were, so setting $b=null does not affect the "$this" that was passed to ObjHandler. A reference remains until the end of the script. ObjHandler::link($a): Because the method's $obj parameter is passed by-reference, you'll get the same behavior with that as you would with the $a2 = &$a you had before - except this time it's $obj = &$a. I don't know the purpose of the ObjHandler but I'm sure that link()'s $obj should not be by-reference. Previous Comments: ------------------------------------------------------------------------ [2013-11-29 10:25:30] erwan dot oger35 at gmail dot com Note that even you change : "$a2 = &$a;" by : "ObjHandler::link($a);" it returns the same result !! ------------------------------------------------------------------------ [2013-11-29 10:04:34] erwan dot oger35 at gmail dot com Description: ------------ --- From manual page: http://www.php.net/language.operators.assignment --- "As of PHP 5, the new operator returns a reference automatically, so assigning the result of new by reference results in an E_DEPRECATED message in PHP 5.3 and later, and an E_STRICT message in earlier versions." Test script: --------------- <?php class A { private $name; private $counter; public function __construct ( $name ) { $this->name = $name; $this->counter = 0; } public function __destruct () { echo __METHOD__. ' : '. $this->name . ' ('.(++$this->counter).')<br/><br/>'.PHP_EOL.PHP_EOL; } } class B { private $name; private $counter; public function __construct ( $name ) { $this->name = $name; $this->counter = 0; ObjHandler::link($this); } public function __destruct () { echo __METHOD__. ' : '. $this->name . ' ('.(++$this->counter).')<br/><br/>'.PHP_EOL.PHP_EOL; } } class ObjHandler { private static $objLinked = array(); public static function link ( &$obj ) { self::$objLinked[] = &$obj; } } $a = new A('a'); $a2 = &$a; $a = null; $b = new B('b'); $b = null; Expected result: ---------------- A::__destruct : a (1) B::__destruct : b (1) FIN Actual result: -------------- A::__destruct : a (1) FIN B::__destruct : b (1) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=66200&edit=1

« previous php.bugs (#183010) next »