# objects passed as reference in php5

- Thread: https://externals.io/message/11998
- Original archive: https://news-web.php.net/php.internals/11998
- Started by Curt Zirzow on 2004-08-08T02:23:53+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #11998 — Curt Zirzow — 2004-08-08T02:23:53+00:00

URL: https://externals.io/message/11998#11998

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!

---

## #11999 — Ron Korving — 2004-08-08T11:14:55+00:00

URL: https://externals.io/message/11998#11999 · In reply to: #11998

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!

---

## #12000 — Marcus Boerger — 2004-08-08T12:37:14+00:00

URL: https://externals.io/message/11998#12000 · In reply to: #11999

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...
> > […]
> schemes
> > […]




-- 
Best regards,
 Marcus                            mailto:helly@php.net

---

## #12040 — Curt Zirzow — 2004-08-09T17:46:07+00:00

URL: https://externals.io/message/11998#12040 · In reply to: #11999

* 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!

---

## #12041 — Marcus Boerger — 2004-08-09T18:28:12+00:00

URL: https://externals.io/message/11998#12041 · In reply to: #12040

Hello Curt,

Monday, August 9, 2004, 7:46:07 PM, you wrote:

> * Thus wrote Ron Korving:
> > […]

> 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
