Search code examples
phpclasscurly-braces

curly braces php on class


I have a simple class... I need to update public $token if it is setted. My code is:

<?php
class Amazon {
    public $token = '';
    public function update($key = '', $value = '')
    {
        if(isset($this->"{$key}"))
        {
            $this->"{$key}" = $value;
        }
        return true;
    }
}

But I am getting this error: Parse error: syntax error, unexpected '"', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'


Solution

  • Try it without the quotes. $this->{$key}