Page 1 of 1

create a variable that is another class inside a class

Posted: Sat Jun 11, 2005 2:27 pm
by flycast
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?

Posted: Sat Jun 11, 2005 2:34 pm
by hawleyjr
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 class2

Posted: Sat Jun 11, 2005 2:34 pm
by mudvein
it's called a reference

Code: Select all

class one
{
   function($blah)
   {
      $blah = &$bob;
   }
}
class two extends one
{
   function(&$bob)
   {
      $something = &$bob;
   }
}
this may not be exactly right, but close enough to matter.