Jump to content

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?

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.