|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-11-29 10:04 UTC] 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) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 07:00:01 2025 UTC |
Hi thanks for your answer. In first I'm agree with you : << $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. >> However, it's my problems. When I read << "As of PHP 5, the new operator returns a reference automatically" >>, I understand that the two ARE the same variable: _______________ _______________________ $this ------->| ID |------>| B OBJECT | $b ------->|_______________| |_______________________| but in fact, it's this is : _______________ _______________________ $this ------->| ID_1 |------>| | |_______________| | | _______________ | B OBJECT | $b ------->| ID_2 |------>| | |_______________| |_______________________| And the goal of ObjHandler's goal it's to destruct all objects (update in db) in case of error (even fatale). So I need only one reference on this object when I want they'll be destructed only once. Actually I need a binding structure, as this singleton example : class MyClass { private static $_instance = null; private $isDestructed; // ************************* // BGN METHODES DE SINGLETON public static function &getInstance () { if ( is_null(self::$_instance) ) { self::$_instance =& self::create(); } return self::$_instance; } // ********************* // BGN CONSTRUCT METHODS private static function &create ( ) { $instance = new static(); ObjHandler::link($instance); return $instance; } private function __construct () { $this->temps_debut=microtime(true); $this->isDestructed = false; } private function __clone(){} // END CONSTRUCT METHODS // ********************* // END METHODES DE SINGLETON // ************************* // ******************** // BGN DESTRUCT METHODS public static function kill ( self &$instance ) { if ( ObjHandler::unlink($instance) ) { $instance = null; return true; } return false; } private function __destruct () { if ( !$this->isDestructed ) { $this->isDestructed = true; // *** TODO : INSTRUCTIONS *** } } // END DESTRUCT METHODS // ******************** // OTHER METHODS ... } So, even it's not a bug, I'll continue to believe it's not a good thing, which should be improved... Thanks.