0

I want to echo the name of the variable object i'm calling, in this case controller_01.

I'm using get_class, but it wont print the variable name, only the object type :(

<?php
class remoteControl{

    private $chip = "Intel64<br />";

    public function openCase(){
        echo "The controler has a " .get_class($this);
        return $this->chip;
    }


}

$control_01 = new remoteControl();


echo $control_01-> openCase();


?> 
Gabriel Meono
  • 990
  • 3
  • 18
  • 48
  • It's not (easily) possible, although there is a project working on it. See my answer on [Is there a way to get the name of a variable? PHP - Reflection](http://stackoverflow.com/questions/1272030/is-there-a-way-to-get-the-name-of-a-variable-php-reflection/4053255#4053255) – Rudu May 27 '11 at 17:57
  • 1
    That's because (1) the name some code uses to refer to an object normally shouldn't bother anyone but that code and (2) The names used to refer to an object totally shouldn't bother the object. –  May 27 '11 at 18:00

1 Answers1

1

You can't do that just like that. An object can have multiple references, but the object isn't itself aware of those references. The only thing you could do, is to enumerate through every variable you can find and check if it points to the object. But those references could exists also in arrays, or in properties of other objects.

And your design is really flawed if you need the object to find its reference variables this way.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210