doubei8541 2013-12-16 14:48
浏览 88
已采纳

PHP是否可以识别包含该对象的变量的名称?

In PHP is it possible to identify the name of the variable containing the object? For example:

class Foo {

    public function IdentifyContainer() {
        //PHP to identify variable containing Foo.
    }

}

$bar = new Foo();
echo $bar->IdentifyContainer(); // This should echo "bar";

I have no idea if this can be done or not, but it would really help me if it can as I have 4 instances of the same class being called on a single page load and I'm trying to identify them all.

EDIT

I should have originally mentioned this is purely for debugging. debug_backtrace worked for me. Thanks to everyone who suggested it :).

  • 写回答

2条回答 默认 最新

  • dorpbn1027 2013-12-16 14:51
    关注

    No, not in any sane way. There's also no good reason to do this whatsoever. No, even if you think you have a good reason, you don't. Since it's perfectly possible to assign an object to several variables at once, there's no intrinsic link between an object (or any value for that matter) and the variable it's assigned to. A variable is not a "container", a variable is a placeholder in an algorithm that lets you refer to changing (variable) values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)