, :
class Settings
{
private $data = array();
public function __call($key,$arguments = array())
{
return !count($arguments)) ? $this->data[$key] : $this->data[$key] = $arguments[0];
}
}
$Settings = new Settings();
$Settings->name('hello');
$Settings->foo('bar');
, , __get __set , ,
class Settings
{
private $data = array();
public function __set($key,$val)
{
$this->data[$key] = $val;
}
public function __get($key)
{
return $this->data[$key];
}
}
:
$Settings->foo = 'bar';
But you can go a little further and create a variable registry system:
so basically you can change the above class from Settingsto VariableStorageand do the following:
class User extends VariableStorage{}
class Profile extends VariableStorage{}
And use like this:
$Profile = new Profile('Robert','Pitt',USER_PRIV_ADMIN);
$Profile->id = 56;
$Profile->userdata = $Database->GetUserData($Profile);
So, you only need to define the methods once and until you create the inheritance structure before a good design, it can work very well.
source
share