Welcome to WebmasterWorld Guest from 54.205.75.60

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

need help with oop, classes logic

   
6:02 am on Oct 24, 2012 (gmt 0)

5+ Year Member



hi guys! I have a situation here that i cant make my mind clear about it.

What i'm doing now is that i have a class named "team" that have a events array inside it. And i create two objects of this class,home and away team. I have another class named "event" that generate different action. At this point everything works well. Where i'm stuck is to whether choose home or away team to assign this event i've created because i want it to be added randomly to home or away team. How could I easily add my event created by my class "event" to my array of the team chose randomly?

I hope i've expressed myself clearly english isnt my first language but i hope im clear enough to get some help!

Thanks!



//TEAM CLASS
class Team {
public $idTeam;
public $events = array();

public function __construct($idTeam) {
$this->idTeam = $idTeam;
}

}

//EVENT CLASS
class Event {
public $name;
public $desc;

public function __construct() {

}

public function genEvent() {
$rand = rand(0,1);

if ($rand == 0) {
$this->name = "Event name 0";
$this->desc = "Event desc 0";
}
elseif($rand == 1) {
$this->name = "Event name 1";
$this->desc = "Event desc 1";
}

return (array)$this;
}

}

//END CLASSES

$awayTeam = new Team(1);
$homeTeam = new Team(2);

$event = new Event();
$myEvent = $event->genEvent();

//CHOOSING RANDOMLY BETWEEN AWAY OR HOME
? i have no clue

8:04 am on Oct 24, 2012 (gmt 0)

5+ Year Member



I'm no PHP expert, but you could that same Random function that generates a random integer between 0 and 1 (inclusive) - like you're already doing.

Then, you could have an IF statement saying IF random result is 0, add to awayTeam, if random result is 1, add to homeTeam.
9:43 am on Oct 24, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how about this:

$var = array("home", "away");
$test = $var[rand(0, count($var)-1)];
11:40 am on Oct 24, 2012 (gmt 0)

5+ Year Member



$test = $var[rand(0, count($var)-1)];

Yup, much nicer.
2:03 pm on Oct 24, 2012 (gmt 0)

10+ Year Member



What exactly you are willing to use ... OOP or classes?

FYI, currently you are using classes and NOT OOP. Just creating classes and (programming) object is not REAL OOP.

Concept of Object-oriented programming is to emulate a real object. Here TEAM is your main object. So you should try to emulate it with it's characteristics and functionalities (operations).

Event should be part of the TEAM object. So it should be something like:-


object_TEAM:
characteristic_1
characteristic_2
characteristic_3
characteristic_4

Object_Event:
characteristic_1
characteristic_2
characteristic_3
characteristic_4

functionality_1
functionality_2
functionality_3
functionality_4

/Object_Event


functionality_1
functionality_2
functionality_3
functionality_4

/object_TEAM



Now this TEAM object can be a home team, away team, or city/country wise (US, UK etc) team, or sports wise team (football, cricket etc). Team event should be a part of that TEAM.

HTH.


PS: If you are writing a public function to access a variable (in your case: $idTeam), you should declare that variable as private. If it's public then why do you need any function to access it?
6:57 pm on Oct 24, 2012 (gmt 0)

5+ Year Member



well what im trying to do exactly is to create event during a hockey game. So there will be shot, penalty, faceoff events etc. And i want to associate these to home or away team to create a simulated hockey game. So there will be multiple "events" class that i want to create, like shot, penalty, injury etc. Everything that could happen during a game. Im having problem figuring out how to handle and seperate all of this. Do i create a team class and create an array for penalty and create a class for penalty and add each penalty event inside my array of my team class.
11:39 pm on Oct 24, 2012 (gmt 0)

10+ Year Member



I guess, you may write simple code like this:

//CHOOSING RANDOMLY BETWEEN AWAY OR HOME

if (rand(1, 2) == 1) {
// Home team
}
else {
// Away team
}

Is there anything I am missing?
11:41 pm on Oct 24, 2012 (gmt 0)

10+ Year Member



>> Do i create a team class and create an array for penalty and create a class for penalty and add each penalty event inside my array of my team class.

I think you need Game class also. That game class will generate event and assign that event to a random team.

No game no penalty, right?
12:50 am on Oct 25, 2012 (gmt 0)

5+ Year Member



when assigning that event to a random team should i pass by reference my team object to my method of my penalty event so i would be able to know who's in the penalty box for that team?