blueteck blueteck - 6 months ago 20
PHP Question

PHP access dynamic attribute from constant content

I want to access a dynamics attribute with the name of the content of a constant. Like

constant($var)
just the other way around.

This is working, but I am wondering is there is something simpler (without
$varname
)?

const CONST_1 = 'var_1';
$this->var_1 = 'Hello World';

$varnam = CONST_1;
echo $this->$varnam;

Answer

Beside my initial comment :

You can do

echo $this->{CONST_1}

if this is what you are looking for

Comments