ts.
ts.

Reputation: 10709

is there any way to access all references to given object?

anyone has idea if and how is it possible to destroy / change php object which is referenced in many places? unset obviously destroys only one reference, and sometimes tracing all references manually is not an option. Any ideas? Maybe there is something i am missing in Reflection ?

Upvotes: 6

Views: 985

Answers (2)

Peter Johnson
Peter Johnson

Reputation: 2703

Nice answer Mark, but i'm not sure how this would work:

First Diagram:

<?php

$obj = "foo";
$a = $obj;
$b = $obj;
$c = $obj;

$c = NULL;
unset( $c );
var_dump( $a, $b, $c );

Results:

string(3) "foo"
string(3) "foo"
NULL

Second Diagram:

<?php

$obj = "foo";
$wrapper =& $obj;
$a = $wrapper;
$b = $wrapper;
$c = $wrapper;

$c = NULL;
unset( $c );
var_dump( $a, $b, $c );

Results:

string(3) "foo"
string(3) "foo"
NULL

Correct Way:

<?php

$obj = "foo";
$a =& $obj;
$b =& $obj;
$c =& $obj;

$c = NULL;
var_dump( $a, $b, $c );

Results:

NULL
NULL
NULL

Explanation:

You need to reference your variables $a,$b,$c to the memory address of $obj, this way when you set $c to NULL, this will set the actual memory address to NULL instead of just the reference.

Upvotes: 3

Mark Byers
Mark Byers

Reputation: 838076

No but you can use an extra level of indirection instead. Currently you have this:

 a    b     c           a    b    (unset)
  \   |    /             \   |
   \  |   /    -->        \  |
    object                 object

Instead you can do this:

 a    b     c           a    b     c
  \   |    /             \   |    /
   \  |   /    -->        \  |   /
   wrapper                (unset)
      |
      |
   object

Upvotes: 6

Related Questions