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.. 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 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? 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. 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! 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? 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 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
Archived
This topic is now archived and is closed to further replies.