php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #6193 new object makes a shallow copy
Submitted: 2000-08-16 07:28 UTC Modified: 2006-04-03 09:36 UTC
From: Heinz at chanet dot de Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.0.1pl2 OS: Win2000
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: Heinz at chanet dot de
New email:
PHP Version: OS:

 

 [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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-08-16 07:48 UTC] 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:55 UTC] 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-17 04:30 UTC] 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-17 08:46 UTC] waldschrott@php.net
urgs, that?s a pity - there?s some unexpected behaviour
using references, we should document this unless it?s not
improved...
 [2006-04-03 09:36 UTC] tony2001@php.net
Fixed in PHP5.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Dec 28 07:00:01 2025 UTC