开发者

How to make a Small system to comment on a individuals in a group in PHP?

I am building a team roster application. It takes a team class (this is a quick mockup not the actual class as my question does not pertain to it)

class Team {
    function __construct($teamName, $roster){
      $this->setName($teamName);
      $this->setRoster($roster);
     }

I didn't include the set functions because this class is the foundation for my question. I want to add a section to comment on each person in the roster. Example:

$roster = array('Jim','Bob','Steve','Josh');
$team = new team('My Team', $roster);

I want each person on the team to have a section where someone can comment on them. Example:

My Team

  • id:1 Jim - add co开发者_StackOverflowmment
  • id:2 Bob - add comment

    Bob needs to come to more practices - delete comment

  • id:3 Steve - add comment
  • id:4 Josh - add comment

My question is this; do I create a comment class and then create a new class for each person? I would think that this would be a bad idea if their where 100+ people. Or do I create functions in my team class to handle the commenting?


Yeah, I would create a "Team member" class and a "Comment" class. why would it be bad to instantiate 100 teamMembers? I might also create a "Roster" class if you ever find a roster is more than just a list of team members...


Zak nailed it. If people can comment on team members, then the most straightforward solution is to make a TeamMember or Player class that has a name property and a comments property. Something like this:

class Team {
  function __construct($teamName, $roster){
    $this->setName($teamName);
    $this->setRoster($roster);
  }
}

class Player {
  function __construct($playerName) {
    $this->setName($playerName);
  }

  function addComment($comment) {
    $this->comments[] = $comment;
  }
}

$jim = new Player('Jim');
$bob = new Player('Bob');
// ...

$roster = array($jim, $bob, ...);
$team = new Team('My Team', $roster);

$bob->addComment("Bob needs to come to more practices!");

And, like Zak says, you could make a Comment class, too, e.g.:

class Comment {
  function __construct($commentText, $from, $date = null) {
    $this->setText($commentText);
    $this->setCommenter($from);

    $date = $date ? $date : new DateTime(); // default to now
    $this->setDate($date);
  }
}

// Jim leaves a comment about Bob
$bob->addComment(new Comment("Bob throws like a girl!", $jim));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜