Re: ability to use extract to $this vars in a class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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;
)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux