scotchegg78 Posted September 3, 2009 Share Posted September 3, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/173033-array-of-classes-in-another-class/ Share on other sites More sharing options...
mikesta707 Posted September 3, 2009 Share Posted September 3, 2009 yeah that will create an array with a length of 10, with each entry holding an object of the class foo. I don't know why you need so many foo's, but that is correct Quote Link to comment https://forums.phpfreaks.com/topic/173033-array-of-classes-in-another-class/#findComment-911991 Share on other sites More sharing options...
scotchegg78 Posted September 3, 2009 Author Share Posted September 3, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/173033-array-of-classes-in-another-class/#findComment-912011 Share on other sites More sharing options...
mikesta707 Posted September 3, 2009 Share Posted September 3, 2009 yes indeed. Quote Link to comment https://forums.phpfreaks.com/topic/173033-array-of-classes-in-another-class/#findComment-912014 Share on other sites More sharing options...
ToonMariner Posted September 3, 2009 Share Posted September 3, 2009 you could decorate the classes as there is a variable number of players in a game - subs etc. could also include match officials in that too! Quote Link to comment https://forums.phpfreaks.com/topic/173033-array-of-classes-in-another-class/#findComment-912023 Share on other sites More sharing options...
scotchegg78 Posted September 3, 2009 Author Share Posted September 3, 2009 decorate the class?! sorry fella whats that? Quote Link to comment https://forums.phpfreaks.com/topic/173033-array-of-classes-in-another-class/#findComment-912039 Share on other sites More sharing options...
Daniel0 Posted September 3, 2009 Share Posted September 3, 2009 decorate the class?! sorry fella whats that? http://en.wikipedia.org/wiki/Decorator_pattern Quote Link to comment https://forums.phpfreaks.com/topic/173033-array-of-classes-in-another-class/#findComment-912064 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.