I have a class called Class1.
I want to have a variable inside Class1 that is an instance of Class2. How do I instantiate Class2 inside Class1 so it is usable throughout my code?
create a variable that is another class inside a class
Moderator: General Moderators
Here is an example where Class2 has an instance of Class1 same thing though...
Code: Select all
class Class1{
var $color;
var $size;
function Class1($somecolor){
$this->color=$somecolor;
}
function getColor(){
return $this->color;
}
function getSize(){
return $this->size;
}
}//end class1
class Class2 extends Class1{
function Class2(){
$Obj1 = new Class1('red');
echo $Obj1->getColor();
}
}//end class2it's called a reference
this may not be exactly right, but close enough to matter.
Code: Select all
class one
{
function($blah)
{
$blah = &$bob;
}
}
class two extends one
{
function(&$bob)
{
$something = &$bob;
}
}