Detecting whether a PHP variable is a reference / referenced

后端 未结 5 1025
情书的邮戳
情书的邮戳 2020-11-27 05:33

Is there a way in PHP to determine whether a given variable is a reference to another variable and / or referenced by another variable? I appreciate that it might not be po

5条回答
  •  长情又很酷
    2020-11-27 06:08

    Edit: It seems I've answered the question 'is it possible to check if two variables are referencing same value in memory' not the actual question asked. :P


    As far as 'plain' variables go the answer is 'no'.

    As far as objects go - maybe.

    All objects are by default handled by references. Also each object has it's serial number which you can see when you var_dump() it.

    >> class a {};
    >> $a = new a();
    >> var_dump($a);
    
    object(a)#12 (0) {
    }
    

    If you could get somehow to this #, you could effectively compare it for two variables, and see if they point to the same object. The question is how to get this number. var_export() does not return it. I don't see snything in Reflection classes that would get it either.

    One thing that comes to my mind is using output buffering + regex

提交回复
热议问题