[2013年10月15日 19:09 UTC] veg at tut dot by
Description: ------------ I've used a little code snippet to convert property from value type to reference type. It's described here: http://www.php.net/manual/en/language.oop5.cloning.php It's very useful practice when you want to share some properties between all clones. But it works with some bugs. When you try to read a reference type property of object without clones, this property unexpectedly converted to value type property. Test script: --------------- <?php header('content-type: text/plain; charset=utf-8'); class demo { public $n = 0; function __construct() { $this->n =& $this->n; } function magic() { if ($this->n) return; } function set($n) { $this->n = $n; } } // Outputs 1 = 1 as expected $a1 = new demo(); $b1 = clone $a1; $a1->magic(); $b1->set(1); echo "{$a1->n} = {$b1->n}\n"; // Outputs 0 = 1 (magic() method is called a little earlier) $a2 = new demo(); $a2->magic(); $b2 = clone $a2; $b2->set(1); echo "{$a2->n} = {$b2->n}\n"; Expected result: ---------------- 1 = 1 1 = 1 Actual result: -------------- 1 = 1 0 = 1 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2013年10月15日 22:07 UTC] bwoebi@php.net
[2018年03月16日 23:04 UTC] cmb@php.net
|