views:

44

answers:

2

Hello

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 ?

+5  A: 

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
Mark Byers
Nice visual answer.
webbiedave
+1  A: 

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.

Peter Johnson