Jump to content

Array of classes in another class ?


scotchegg78

Recommended Posts

hi guys

 

If i want an array of classes as a variable within a class is this correct..

 

 

class foo {

}


class bar{


private foos= array();

function __construct()
{

for($i = 0; $i < 10; $i++) {
$this->foos[] = new foo;
}

}
}

 

Is this right? I am trying to readup on this aspect of OO to recap but cannot find any reference to this requirement if anyone knows of a book or site?

 

thanks for any tips..

 

Link to comment
https://forums.phpfreaks.com/topic/173033-array-of-classes-in-another-class/
Share on other sites

thanks

 

10 was just an example number, its the idea of modeling something within something.

Think footballers = foo, bar = game of football etc.

a game has 10 instances of footballers.

 

how would i access say the 4th instance in the array of classes?

 

$this->foos[3]->getid();

 

for example?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.