|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2000-08-16 07:28 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 28 07:00:01 2025 UTC |
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;3) possibly related, just realized that this does not work too... class CTest { var $v=9; function ®() { return $this; } } $myobj= new CTest(); $register_object=$myobj->reg(); var_dump($register_object);