Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 738
ts. Avatar asked May 23 '10 22:05

ts.


2 Answers

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
like image 88
Mark Byers Avatar answered Sep 19 '22 00:09

Mark Byers


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.

like image 31
Peter Johnson Avatar answered Sep 18 '22 00:09

Peter Johnson