create a variable that is another class inside a class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
flycast
Forum Commoner
Posts: 37
Joined: Wed Jun 01, 2005 7:33 pm

create a variable that is another class inside a class

Post 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?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post 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.
Post Reply