开发者

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

开发者 https://www.devze.com 2022-12-31 14:28 出处:网络
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 refer

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 ?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消