|
|
#1 (permalink) |
|
Honorary Member
Join Date: Jun 2006
Age: 32
Posts: 1,039
Rep Power: 4
|
Are there any rules for classes and inheritance?
e.g. currently i have 4 classes - logparser - match_stats (parent class) - team_stats (child class) - players_stats The issue i have is sending the log array to each class line by line. As the child inherits the parent i have to start the log parsing in the lowest child. Example Code class team_stats extends match_stats { function get_data($log){ $this->log = $log; foreach ($this->log as $data) { $this->data = $data; $this->check_start($data); <-- this refers to a method in the parent class $this->score($data); } } ................................ Instantiated as below $teams = new team_stats(); $teams->get_data($logparser->log); Do i have too many classes or going about it the wrong way?? I want to be able go through an array of the game log line by line stepping through the following classes - match stats - team stats - player stats before going to the next line in the array ($logparser->log). Thanks for any help Last edited by billy baxter; 09-07-2007 at 07:02 PM.. |
|
|
|
|
|
|
|
#2 (permalink) |
|
Pro Member
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
|
Can you not just call the base class's definition before the derived's definition?
There are no real rules.. just good practices. Since this is PHP you might want to ask Khaless personally about the best practices (since he's like, some sort of PHP/Perl/WebDev god )EDIT: re-read your post. chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots. - Last edited by chemicalNova; 09-07-2007 at 08:22 PM.. |
|
|
|
|
|
#3 (permalink) | |
|
Honorary Member
Join Date: Jun 2006
Age: 32
Posts: 1,039
Rep Power: 4
|
Quote:
in php a parent class cannot call a childs method and properties e.g. i cannot call the function score() in the team_stats class (child) from the match_stats class (parent). |
|
|
|
|
|
|
#4 (permalink) |
|
Pro Member
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
|
Your PHP confuses the hell out of me.. so I'm not quite sure how to explain what I meant..
The way I'd do it is just have 4 separate classes. Team stats doesn't have to inherit from match_stats.. does it? What does match stats involve? I'd possibly have one "parent" class, which just owned objects of the other classes, instead of them inheriting from it. The parent would then grab the log, and dish out whatever was needed to the attributes of the parent.. so basically, the "child" classes would just be data holders.. no methods, just full of variables. ..thats what I'd do anyway. chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots. - |
|
|
|
|
|
#5 (permalink) | |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
Yes, There are many, many, MANY. (and many schools of thought) start...
http://en.wikipedia.org/wiki/Liskov_...tion_principle http://en.wikipedia.org/wiki/Design_...mputer_science) Is-a, Has-a... Quote:
match stats has 2 team stats, team stats is not a match_stats team_stats has player stats which is not a team_stats or by c.def. match_stats Perhaps build match_stats, team_stats and player_stats and some sort of delegation perhaps handled by a logentry obj and passed down depending on your structure. see it would be nice if match stats could just aggregate team stats of which aggregates player stats. and then conceivably all world events (suicides, restarts etc are captured by match stats) others by containing team_stats by delegation downwards and the rest kills/headshots in player_stats. then you could ask match_stats how many head shots were in a match and it would (hidden implementation to the top level programmer) ask the teams for their total hs, which ask players for their total hs.. etc etc etc. its to late and im tired,hope you get the gist.
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. Last edited by Khaless; 09-07-2007 at 11:13 PM.. |
|
|
|
|
|
|
#6 (permalink) |
|
Honorary Member
Join Date: Jun 2006
Age: 32
Posts: 1,039
Rep Power: 4
|
I have match stats as showing me the state of the game
class match_stats{ public $start = "no"; public $end = "no"; public $map; public $max_rnds; public $overtime_swap_sides; public $score_to_win; public $half = 0; public $overtime = "no"; public $current_round = 0; team stats show me stats related to teams class team_stats extends match_stats { public $name; public $side; public $score = 0; public $team1 = "CT"; public $team2 = "Terrorist"; public $match_score = array(); public $round_score = array(); public $data; player stats show me stats related to players class player_stats extends team_stats { public $name; public $steamid; public $kills; public $deaths; public $damages; etc etc .. I guess as they both need to refer to match stats for the state of the match i was thinking inheritance, but after thinking about it a match isnt a team and a team isnt a player. I think i can use a class or something to send the data out to each class if the match has started. Thanks for reading Khaless i will look at it sometime this week, only started reading about classes on Saturday, still trying to grasp the very basics. |
|
|
|
|
|
#7 (permalink) |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
I think that
team, match and player stats are all individual objects But perhaps they are all LogReceivers. So one might say class LogReceiver {} then: class PlayerStats Extends LogReceiver class TeamStats Extends LogReceiver class MatchStats Extends LogReceiver Then we could have a list of matches with teams in them and teams with players in them, get all the objects buried inside and just loop through them sending them logs, and they will update themselfs, which in turn, because match stats say holds a team stats and on a query to match stats match queries team, it will always produce the right results.
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. |
|
|
|
|
|
#8 (permalink) |
|
Honorary Member
Join Date: Jun 2006
Age: 32
Posts: 1,039
Rep Power: 4
|
Yeah was thinking something similiar on the way to work, anyway will give me something to play with.
Can you call another object from within an class without instantiation within the class that calls? eg $outside_class = new outsideclass(); class someclass { //properties ...... // methods ........ function somefunction ($data) { $this->data = $data; $outside_class->help($data); } } // end someclass class outsideclass { //properties ...... // methods ........ function help($noidea) { whatever you want here } } // end outsideclass |
|
|
|
|
|
#9 (permalink) | |
|
Pro Member
Join Date: May 2006
Location: Sydney, Australia
Posts: 6,539
Rep Power: 9
|
Quote:
__________________
OM NOM NOM |
|
|
|
|
|
|
#11 (permalink) | |
|
Pro Member
Join Date: May 2006
Location: Sydney, Australia
Posts: 6,539
Rep Power: 9
|
Quote:
![]() http://www.php.net/manual/en/languag...ekudotayim.php
__________________
OM NOM NOM |
|
|
|
|
|
|
#16 (permalink) |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
The concepts of OO design as similar in a number of languages. The majority of what we talk about here are not specific to PHP.
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. |
|
|
|
|
|
#17 (permalink) | |
|
Honorary Member
Join Date: Jun 2006
Age: 32
Posts: 1,039
Rep Power: 4
|
Quote:
e.g. as the logreceiver goes through line be line to send out to the child classes wouldnt the log receiver need the three objects (team,player,stats), inside itself.?? sorry im new and trying to learn, have googled it a bit and only references i seem to find are like http://www.ibm.com/developerworks/op.../os-advphpobj/ http://nefariousdesigns.co.uk/archiv...relationships/ Last edited by billy baxter; 10-07-2007 at 09:35 PM.. |
|
|
|
|
|
|
#18 (permalink) |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
Well, My notion there was saying that
1) Player is not a Team and Team is not a Match and 2) perhaps though they do not share that relationship, they do share another, which is they all accept log data and handle/manip. it So one might say class LogReceiver { parseLogLine(); _parseLogHelper(); _parseLogVar; } and then create the other classes as instances of these class PlayerStats extends LogReceiver {} then one could have a list of LogReceiver objects (player,team,stats) and feed lines to all of them and they would be capable of parsing them or indeed feed it to match, which feeds it to team through a standard interface (defined by the parent) This way they would have a class heirachy too. though clearly not what you would expect off the bat. Look up the Observer and Observable design pattern as well for a different (better) better idea: http://www.exciton.cs.rice.edu/JAvaR...Observable.htm etc. (Google it)
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. |
|
|
|
|
|
#19 (permalink) |
|
Honorary Member
Join Date: Jun 2006
Age: 32
Posts: 1,039
Rep Power: 4
|
Thanks again , i just need a little nudge in the right direction, sinking with info overload atm.
Found some more links below about design patterns, will read up when im not tired. http://www-128.ibm.com/developerwork...p-designptrns/ http://www.phppatterns.com/docs/design/observer_pattern http://www.zend.com/php5/articles/ph...ypes_Interface Last edited by billy baxter; 11-07-2007 at 08:34 PM.. |
|
|
|
|
|
#20 (permalink) |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
Just a consideration,
Following patterns may be a bit overkill for your project. You may want to adapt . Just so you know, this is not a one and only way
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. |
|
|
|



)