objects passed as reference in php5

php.internals

Curt Zirzow

22 years ago
I'm trying to document how objects are passed to functions in php5, but am running into some troubles. In the code below the object appears to be a reference but when the original object is destoyed it still somehow exists. Can someone explain how this is? I was going to report a bug on this but using the traditional &$obj usage it works perfectly fine. class Test { public $object = null; public $object1 = null; function SetVarVal($obj) { $this->object = $obj; } function SetVarRef(&$obj) { $this->object1 = &$obj; } } $obj1 = new StdClass(); $obj2 = new Test(); $obj2->SetVarVal($obj1); $obj2->SetVarRef($obj1); $obj1->foo = 'test'; /* show how obj1's is a ref */ $obj1 = null; /* destroy object */ var_dump($obj2->object); /* still has object with ->foo */ var_dump($obj2->object1); /* NULL */ Curt
-- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid!

Ron Korving

22 years ago
i think the object will only cease to exist when all references to it are gone "Curt Zirzow" <curt@php.net> wrote in message news:20040808022353.GG6522@bagend.shire...
> > I'm trying to document how objects are passed to functions in php5, > but am running into some troubles. > > In the code below the object appears to be a reference but when the > original object is destoyed it still somehow exists. Can someone > explain how this is? I was going to report a bug on this but using > the traditional &$obj usage it works perfectly fine. > > class Test { > public $object = null; > public $object1 = null; > > > function SetVarVal($obj) { > $this->object = $obj; > } > > function SetVarRef(&$obj) { > $this->object1 = &$obj; > } > > } > > $obj1 = new StdClass(); > > $obj2 = new Test(); > $obj2->SetVarVal($obj1); > $obj2->SetVarRef($obj1); > > > $obj1->foo = 'test'; /* show how obj1's is a ref */ > $obj1 = null; /* destroy object */ > > var_dump($obj2->object); /* still has object with ->foo */ > var_dump($obj2->object1); /* NULL */ > > > > Curt > -- > First, let me assure you that this is not one of those shady pyramid
schemes

Marcus Börger

22 years ago
Hello Ron, that's prefectly correct. regards marcus Sunday, August 8, 2004, 1:14:55 PM, you wrote:
> i think the object will only cease to exist when all references to it are > gone
> "Curt Zirzow" <curt@php.net> wrote in message > news:20040808022353.GG6522@bagend.shire... >> >> I'm trying to document how objects are passed to functions in php5, >> but am running into some troubles. >> >> In the code below the object appears to be a reference but when the >> original object is destoyed it still somehow exists. Can someone >> explain how this is? I was going to report a bug on this but using >> the traditional &$obj usage it works perfectly fine. >> >> class Test { >> public $object = null; >> public $object1 = null; >> >> >> function SetVarVal($obj) { >> $this->object = $obj; >> } >> >> function SetVarRef(&$obj) { >> $this->object1 = &$obj; >> } >> >> } >> >> $obj1 = new StdClass(); >> >> $obj2 = new Test(); >> $obj2->SetVarVal($obj1); >> $obj2->SetVarRef($obj1); >> >> >> $obj1->foo = 'test'; /* show how obj1's is a ref */ >> $obj1 = null; /* destroy object */ >> >> var_dump($obj2->object); /* still has object with ->foo */ >> var_dump($obj2->object1); /* NULL */ >> >> >> >> Curt >> -- >> First, let me assure you that this is not one of those shady pyramid > schemes >> you've been hearing about. No, sir. Our model is the trapezoid!
-- Best regards, Marcus mailto:helly@php.net

Curt Zirzow

22 years ago
* Thus wrote Ron Korving:
> i think the object will only cease to exist when all references to it are > gone
Well there does seem to be another layer of referencing, since the a simple assignment without &$ will simply reference the object but in a different way. The problem I'm running into there doesn't seem to be a way to reliably detstroy an object without the programmer keeping track of the variable assignments and removing those references as well. I'm not sure if its possible or even php's responsibility but perhaps provide a tool that allow's a programmer to explicity destroy an object? with something like: Destroy($obj); Curt
-- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid!

Marcus Börger

22 years ago
Hello Curt, Monday, August 9, 2004, 7:46:07 PM, you wrote:
> * Thus wrote Ron Korving: >> i think the object will only cease to exist when all references to it are >> gone
> Well there does seem to be another layer of referencing, since the > a simple assignment without &$ will simply reference the object but > in a different way. The problem I'm running into there doesn't > seem to be a way to reliably detstroy an object without the > programmer keeping track of the variable assignments and removing > those references as well.
> I'm not sure if its possible or even php's responsibility but > perhaps provide a tool that allow's a programmer to explicity > destroy an object? with something like:
> Destroy($obj);
This is not possible at all. Reference counting + garbage collection means we do know the exact number of references to an object. Destroying the referenced object before its reference count goes to zero means that we leave a reference pointing to nirvana. That would result in a SEGV. regards marcus

Andi Gutmans

22 years ago
Yep. It was decided not to go down that route. You should have a good enough handle of your application to know where you are referencing global objects from (local ones are destroyed once a function is terminated). If you don't, well, maybe try and improve the code which deals with global objects so that you have a better grasp of where they are. There are various ways of achieving that. Andi At 08:28 PM 8/9/2004 +0200, Marcus Boerger wrote: