Search code examples
phpmagic-constantsobject-notation

Using magic constants to access object properties


I am about to load a language object as a property from controller object like this:

$this->lang = new Languages(en)

Inside the methods of my controller object i am currently accessing the specific translation like this:

function = myFunction(){

$magic = __FUNCTION__;
$lang = $this->lang->$magic;

~~ mycode

}

BUT i want something like this to make it "leaner" code:

$lang = $this->lang->__FUNCTION__;

Does anybody know how to use the Magic Constants in object notation properly ? Unfortunately i havent found any answer here or official php.net website


Solution

  • as @AbraCadaver suggested, $this->lang->{__FUNCTION__} is working