homepage Welcome to WebmasterWorld Guest from 54.204.94.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
need help with oop, classes logic
skoff




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

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


 

bhonda




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

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.

omoutop




msg:4511610
 9:43 am on Oct 24, 2012 (gmt 0)

how about this:

$var = array("home", "away");
$test = $var[rand(0, count($var)-1)];

bhonda




msg:4511659
 11:40 am on Oct 24, 2012 (gmt 0)

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

Yup, much nicer.

Dinkar




msg:4511700
 2:03 pm on Oct 24, 2012 (gmt 0)

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?

skoff




msg:4511879
 6:57 pm on Oct 24, 2012 (gmt 0)

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.

Dinkar




msg:4512011
 11:39 pm on Oct 24, 2012 (gmt 0)

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?

Dinkar




msg:4512012
 11:41 pm on Oct 24, 2012 (gmt 0)

>> 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?

skoff




msg:4512031
 12:50 am on Oct 25, 2012 (gmt 0)

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?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved