Old 09-07-2007   #1 (permalink)
Honorary Member
 
Join Date: Jun 2006
Age: 32
Posts: 1,039
Rep Power: 4
Default Classes - Any rules

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..
billy baxter is offline   Reply With Quote
Old 09-07-2007   #2 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

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..
chemicalNova is offline   Reply With Quote
Old 09-07-2007   #3 (permalink)
Honorary Member
 
Join Date: Jun 2006
Age: 32
Posts: 1,039
Rep Power: 4
Default

Quote:
Originally Posted by chemicalNova View Post
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
not sure what you mean?

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).
billy baxter is offline   Reply With Quote
Old 09-07-2007   #4 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

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.
-
chemicalNova is offline   Reply With Quote
Old 09-07-2007   #5 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

Quote:
Originally Posted by billy baxter View Post
Are there any rules for classes and inheritance?
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:
Originally Posted by billy baxter View Post

e.g.

currently i have 4 classes

- logparser
- match_stats (parent class)
- team_stats (child class)
- players_stats
Does match_stats and team stats actually form a type heirachy, i do not think they do
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..
Khaless is offline   Reply With Quote
Old 10-07-2007   #6 (permalink)
Honorary Member
 
Join Date: Jun 2006
Age: 32
Posts: 1,039
Rep Power: 4
Default

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.
billy baxter is offline   Reply With Quote
Old 10-07-2007   #7 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

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.
Khaless is offline   Reply With Quote
Old 10-07-2007   #8 (permalink)
Honorary Member
 
Join Date: Jun 2006
Age: 32
Posts: 1,039
Rep Power: 4
Default

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
billy baxter is offline   Reply With Quote
Old 10-07-2007   #9 (permalink)
Pro Member
 
nuk3's Avatar
 
Join Date: May 2006
Location: Sydney, Australia
Posts: 6,539
Rep Power: 9
Default

Quote:
Originally Posted by billy baxter View Post
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
If you're working with parent and child classes you can probably use the scope resolution operator to call a static method. But then again, I'll just shut up and stop pretending I understand anything because Khaless will come along with his ninja coding skills and make me look stupid. : P
__________________
OM NOM NOM
nuk3 is offline   Reply With Quote
Old 10-07-2007   #10 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

What is PHP's scope resolution operator? I've never seen :: in PHP so that intrigues me

chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots.
-
chemicalNova is offline   Reply With Quote
Old 10-07-2007   #11 (permalink)
Pro Member
 
nuk3's Avatar
 
Join Date: May 2006
Location: Sydney, Australia
Posts: 6,539
Rep Power: 9
Default

Quote:
Originally Posted by chemicalNova View Post
What is PHP's scope resolution operator? I've never seen :: in PHP so that intrigues me

chem
It's ::

http://www.php.net/manual/en/languag...ekudotayim.php
__________________
OM NOM NOM
nuk3 is offline   Reply With Quote
Old 10-07-2007   #12 (permalink)
Pro Member
 
empske's Avatar
 
Join Date: Jan 2007
Location: ZULU
Age: 17
Posts: 6,719
Rep Power: 0
Default

I don't understand any of that, nuk3.
__________________

odin : atleast i didnt join gotgames in 2009

C:\Documents and Settings\Admin\Desktop\DdudFACE.png
empske is offline   Reply With Quote
Old 10-07-2007   #13 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

Quote:
Originally Posted by nuk3 View Post
Ah, fascinating.. learn something new every day

chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots.
-
chemicalNova is offline   Reply With Quote
Old 10-07-2007   #14 (permalink)
Banned
 
Join Date: Feb 2007
Location: Sydney
Age: 19
Posts: 4,205
Rep Power: 0
Default

I start PHP next week.. oh dear god
Blake is offline   Reply With Quote
Old 10-07-2007   #15 (permalink)
Honorary Member
 
Join Date: Jun 2006
Age: 32
Posts: 1,039
Rep Power: 4
Default

i learnt from a book , hence my noobness
billy baxter is offline   Reply With Quote
Old 10-07-2007   #16 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

Quote:
Originally Posted by Blake View Post
I start PHP next week.. oh dear god
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.
Khaless is offline   Reply With Quote
Old 10-07-2007   #17 (permalink)
Honorary Member
 
Join Date: Jun 2006
Age: 32
Posts: 1,039
Rep Power: 4
Default

Quote:
Originally Posted by Khaless View Post
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
Im confused how the LogReceiver (parent) sends out log data to the child?

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..
billy baxter is offline   Reply With Quote
Old 10-07-2007   #18 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

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.
Khaless is offline   Reply With Quote
Old 10-07-2007   #19 (permalink)
Honorary Member
 
Join Date: Jun 2006
Age: 32
Posts: 1,039
Rep Power: 4
Default

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..
billy baxter is offline   Reply With Quote
Old 11-07-2007   #20 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

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.
Khaless is offline   Reply With Quote