#6193 [Ana->Csd]: new object makes a shallow copy

From: Date: Mon, 03 Apr 2006 07:36:56 +0000
Subject: #6193 [Ana->Csd]: new object makes a shallow copy
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-95232@lists.php.net to get a copy of this message
ID: 6193 Updated by: tony2001@php.net Reported By: Heinz at chanet dot de -Status: Analyzed +Status: Closed Bug Type: Feature/Change Request Operating System: Win2000 PHP Version: 4.0.1pl2 New Comment: Fixed in PHP5. Previous Comments: ------------------------------------------------------------------------ [2000-08-17 08:46:03] waldschrott@php.net urgs, that´s a pity - there´s some unexpected behaviour using references, we should document this unless it´s not improved... ------------------------------------------------------------------------ [2000-08-17 04:30:49] stas@php.net From the talk with Andi it seems that there's no way to do the thing you wnt in PHP, because reference is not a pointer, it's a binding between two variables. SO I move it to feature requests. ------------------------------------------------------------------------ [2000-08-16 07:55:18] waldschrott@php.net 3) possibly related, just realized that this does not work too... class CTest { var $v=9; function &reg() { return $this; } } $myobj= new CTest(); $register_object=$myobj->reg(); var_dump($register_object); ------------------------------------------------------------------------ [2000-08-16 07:48:49] waldschrott@php.net Right, that´s two bugs here... the only proper way I know of to workaround this is to reference them outside, like this: $x = new A(); $c_x = &$x; 1) neither copying nor referencing works in this case inside the constructor, I remember this as a possibly known issue... 2) re-referencing does not work... I´ve moved that linking process to another method (not constructor) - both referencing AND copying works *inside* CTest->Reg, but zend fails to recognize that $reg is already a reference, thus it is not visible outside I think even this is a known issue, not sure $register_object; class CTest { var $v=9; function CTest() { } function reg(&$reg) { $reg=$this; var_dump($reg); } } $myobj= new CTest(); $myobj->reg($register_object); var_dump($register_object); $myobj->v= 1; $register_object->v= 2; echo 'both should be equal: ', $myobj->v, ' ', $register_object->v; ------------------------------------------------------------------------ [2000-08-16 07:28:09] Heinz at chanet dot de The new object function seems to generate the object twice. This is a quite unexpected oo feature and it could be a performance bottleneck for complex classes! test script: <? $register_object; class CTest { var $v; function CTest(&$reg) { $reg= $this; // no difference for: $reg= &$this; } } $myobj= new CTest($register_object); $myobj->v= 1; $register_object->v= 2; echo 'both should be equal: ', $myobj->v, ' ', $register_object->v; ?> I would assume that $myobj and $register_object are the same class instance, but this is not true. Ok, this example is stupid, but assume CTest is a shopping_item and $register_object is a shopping_card. The shopping item in the constructer tries to register itself in the shopping cart. Doesn't this makes sense? In general it's from the SW design point of view critical to handle objects as values and not as refernces. Ok, you introduced in PHP4 the & operator but I've not found how to specify the & operator for the new function. Thanks in advance Heinz ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=6193&edit=1

« previous php.bugs (#95232) next »