in my class i have an array defined like this
class t {
var $settings = array();
}
I will have these settings used quite a lot, so instead of writing $this->settings['setting'] all over the place I wanted to deploy a function that automatically defines these settings in local variables.
private function get_settings () {
$array = $this->settings['array'];
$foreign_key = $this->settings['foreign_key'];
$limit = $this->settings['limit'];
$tableclassid = $this->settings['tableclassid'];
$pager = $this->settings['pager'];
$container = $this->settings['container'];
$extracolumn = $this->settings['extracolumn'];
}
now, what I want to do is to get these variables and use them in another function within class. In example
public function test () {
$this->get_settings();
return $foreign_key;
}
and I want it to return $this->settings['foreign_key']
is there a way to do this? Or do I have to clutter all the functions with that block of code get_settings() has?
I appreciate the help.. thanks 🙂
Use the built-in
extract()function, which extracts an array into individual variables in the current scope.If you need modifications to these local variables to be reflected in the original array, extract them as references.
I can’t say I would prefer to use this method myself though, or even recommend that you do it. Inside of a class, it is much more readable and understandable to keep them in the array property. In general, I never actually use
extract().