I tried to use the content of a class member to access a method variable:
protected function method() {
$var1 = 'no';
$var2 = 'no';
if ($this->data['x']['y'] != 'bums') {
$$this->data['x']['y'] = 'yes';
${$this->data['x']['y']} = 'yes';
}
}
$this->data['x']['y']
can have the contents var1 or var2 in this case.
But why can't I use that way to access the method variables?
Variable variables can be a security risk. Even if you check them, there's no point in opening up another vector where you can make a mistake. Use arrays instead, they're easier to understand and cleaner anyway.
protected function method() {
$var = array(
1 => 'no',
2 => 'no',
);
if ($this->data['x']['y'] != 'bums') {
$var[$this->data['x']['y']] = 'yes';
}
}
It is always a bad and unsecure practise to use $$whatever:
It would be much better to change to code to:
protected function method() {
$var1 = 'no';
$var2 = 'no';
switch ($this->data['x']['y']) {
case 'var1':
$var1 = 'yes';
break;
case 'var2':
$var2 = 'yes';
break;
}
}
HTH, Andreas