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();


?> 
2
  • 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 Commented May 27, 2011 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. Commented May 27, 2011 at 18:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.