alpha_juno wrote:The reason why, is that I do not want the dynamically created attribute to be gotten or set directly. Only via getter and setter methods, allowing me some encapsulation and preserving (as much as possible) the integrity of the objects data structure.
That still does not make sense to me, since the very act of dynamically creating attributes appears to go against the desire to preserve "the integrity of the objects data structure".
What I suggest is to use an associative array, e.g.,
class X
{
private $attributes = array();
public function get($attribute_name)
{
return $this->attributes[$attribute_name];
}
public function set($attribute_name, $attribute_value)
{
$this->attributes[$attribute_name] = $attribute_value;
}
}
$x = new X;
$x->set('variable_name', '123');
echo $x->get('variable_name');