Re: ability to use extract to $this vars in a class
| From: | Guillermo Rauch | Date: | Wed, 09 Feb 2005 03:34:31 +0000 |
| Subject: | Re: ability to use extract to $this vars in a class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-208317@lists.php.net to get a copy of this message | ||
If i understand you correctly, you want to extract all the keys and
generate class members with them..
// Define class test
class test {
// We pass an array to the constructor
function __construct( $arr ) {
foreach($arr as $key => $val ) {
$this->{$key} = $val;
}
// For this example, i print the structure of the object
print_r($this);
}
}
$tests = array( 'hi' => 'bye', 'hey' => 'ho',
'lets' => 'go');
$test = new test($tests);
I forgot in the previous message to mention that if the member exists,
it will be overriden. In addition, you shouldn't use this, as you
don't have control over the accessing to the vars. Instead, you should
store them in a previously defined array (for example private $_vars;
)