Zibings
12-12-2003, 08:06 AM
This isn't so much a question as it is a notification. I want to see if I'm just slow to catch on to things here, or if this is really something that hasn't been "found" before.
I was playing around with a script I'm making, and I thought to myself: "Wow, it'd be soo nice if I could make those classes have dynamic variable declaration." And lo and behold, with a little bit of playing around, I found out I could have my wish! The code looks like this (well, what I did to test it at least):
<?php
class testobj2
{
var $test1;
var $test2;
}
$obj =& new testobj2;
$obj->test1 = 1;
$obj->test2 = array (
'key' => 'val',
'keyset' => array('key1'=>'val1', 'key2'=>'val2')
);
$vars = array (
'name' => 'Andy',
'text' => 'is in love.',
'id' => 2,
'obj' => $obj
);
class tester
{
function tester ( $var_list )
{
foreach ( $var_list as $key => $val )
$this->$key = $val;
$this->name = "Andy";
}
}
$test =& new tester($vars);
echo ( "<pre>" );
print_r($test);
?>
Am I slow? Or was this something that was kept quiet because it could have serious implications for "unsafe" coding? Not really sure which it is, but I'm horribly curious.
PS - If you don't believe that it works: Check this out! (http://www.zibings.com/class_test.php)
I was playing around with a script I'm making, and I thought to myself: "Wow, it'd be soo nice if I could make those classes have dynamic variable declaration." And lo and behold, with a little bit of playing around, I found out I could have my wish! The code looks like this (well, what I did to test it at least):
<?php
class testobj2
{
var $test1;
var $test2;
}
$obj =& new testobj2;
$obj->test1 = 1;
$obj->test2 = array (
'key' => 'val',
'keyset' => array('key1'=>'val1', 'key2'=>'val2')
);
$vars = array (
'name' => 'Andy',
'text' => 'is in love.',
'id' => 2,
'obj' => $obj
);
class tester
{
function tester ( $var_list )
{
foreach ( $var_list as $key => $val )
$this->$key = $val;
$this->name = "Andy";
}
}
$test =& new tester($vars);
echo ( "<pre>" );
print_r($test);
?>
Am I slow? Or was this something that was kept quiet because it could have serious implications for "unsafe" coding? Not really sure which it is, but I'm horribly curious.
PS - If you don't believe that it works: Check this out! (http://www.zibings.com/class_test.php)