Classes within classes (Should I do this?)
| From: | Cameron Just | Date: | Wed, 05 Dec 2001 05:54:09 +0000 |
| Subject: | Classes within classes (Should I do this?) | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-76650@lists.php.net to get a copy of this message | ||
Hi,
Is this a bad thing to do have a class stored within another class?
I would also like to store an array of another class within a class.
It seems to work apart from the fact that I can't reference an array of
class directly.
ie
$test_array[0]->get();
This gives an error.
Example code.
-----------------------------------------------------
<?
class sub_level {
var $testing = 0;
function set_test($temp){
$this->testing = $temp;
}
function get_test(){
return $this->testing;
}
}
class top_level {
var $testing = 0;
var $sub_class = "";
function set($temp){
$this->testing = $test;
}
function get(){
return $this->testing;
}
}
$test = new sub_level;
$top = new top_level;
$top2 = new top_level;
$top->sub_class = $test;
$top2->sub_class = $test;
$top->sub_class->set_test(50);
$top2->sub_class->set_test(1000);
$test_array[0] = $top;
$test_array[1] = $top2;
echo get_class($test_array[0])."<BR>";
echo get_class($test_array[1])."<BR>";
echo get_class($test_array[2])."<BR>";
$temp = $test_array[1];
echo get_class($temp->sub_class)."<BR>";
echo "Booo - ".$temp->sub_class->get_test();
?>
---------------------------------