maca134 Posted March 25, 2010 Share Posted March 25, 2010 Hey Im trying to set some vars in a class, here is code: class someClass { public $var1 = ''; public $var2 = ''; public function __construct($options) { if (isset($options) && is_array($options)) { foreach ($options as $key => $val) { $this->$$key = $val; } } } } $options = array('var1' => 1, 'var2' => 2); $class = new someClass($options); I know the above is wrong but can someone tell me how to access class vars, like you can access vars with a double dollar sign ($$vars) Thanks in advanced Quote Link to comment https://forums.phpfreaks.com/topic/196477-setting-class-vars/ Share on other sites More sharing options...
KevinM1 Posted March 25, 2010 Share Posted March 25, 2010 Why not just: foreach ($options as $key => $value) { $this->$key = $value; } Quote Link to comment https://forums.phpfreaks.com/topic/196477-setting-class-vars/#findComment-1031611 Share on other sites More sharing options...
maca134 Posted March 25, 2010 Author Share Posted March 25, 2010 Cheers I was having a dumb moment lol Quote Link to comment https://forums.phpfreaks.com/topic/196477-setting-class-vars/#findComment-1031613 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.