开发者

Accessing an Object's Creator

开发者 https://www.devze.com 2023-02-12 04:34 出处:网络
Suppose I\'m working with an object with a Has-A relationship with another object - a Game has a list of Players associated with it.From time to time, a Player needs to update his information in the r

Suppose I'm working with an object with a Has-A relationship with another object - a Game has a list of Players associated with it. From time to time, a Player needs to update his information in the roster database - the name of which is a property of the Game. What's the best way to allow the Player to update his own record in the Game's database?

I suppose I could have the Game do the updating for the Player, but I figured it was preferable to have the Players doing their own work whenever possible, and it's his record in the DB开发者_JAVA百科 that needs updating. Besides, I would still need a way for any given Player to know which Game it is a part of. Should each Player have a game indicator embedded as a property within them?

If it helps, here's the relevant code:

class HvZPlayer extends User{
private $bitten;
private $died;
private $hvz_status;
private $lastFed;
private $parent;

public function __construct($data){
    parent::__construct($data);
}

public function set_Human(){
    $this->hvz_status   = "Human";
    $this->bitten       = NULL;
    $this->died         = NULL;
    $this->lastFed      = NULL;
    $this->parent       = NULL;

    $conn = new WritePDO();
    $sql = "UPDATE "; //This is where I'm stuck.
}

}


First of all respect single responsibility principle. It's one of the most important rule of OOP.

According to this simply principle player object, which represents a single player, should not handle things like saving data to the database. However sending a simple messages like Hey, my status has been changed. Maybe you want to do something with me? seems to be perfectly fine. Other object that want to listen to Player object might then do something. Here's where the observer pattern come in. You can try out EventDispatcher from Symfony - a really good implementation of this pattern.

pseudo-code:

$database = ...;
$eventDispatcher = new EventDispatcher();

$eventDispatcher->connect('player.status_changed', function(Event $event) use ($database) {
    $pid    = $event->getSubject()->getId();
    $status = $event->getSubject()->getStatus();

    $database->query('UPDATE player SET status = :stats WHERE id = :id;', array(
        'id'     => $pid,
        'status' => $status
    ));
});

$player = new Player($eventDispatcher);

---------

class Player {
    $eventDispatcher;

    public function __construct(EventDispatcher $eventDispatcher) {
        $this->...;
    }

    public function setStatus($status) {
        $this->status = $status;

        $this->eventDispatcher->notify(new Event($this, 'user.status_changed'));
    }
}


The way I'm toying with doing it now is by using a static method in the Game class to generate an instance with the data:

static function currentGame(){
    $conn = new ReadPDO();
    $sql = "SELECT *
            FROM games
            WHERE id={$_GET['id']}";

    $result = $conn->query($sql);
    $rowCount = $conn->query("SELECT FOUND_ROWS()")->fetchColumn();

    if($rowCount == 1){
        $game = new HvzGame($result->fetch());
        return $game;
    }else{
        return FALSE;
    }
}

This seems a little awkward to me, however, as it will return the currently viewed game, rather than the game that a given Player is a part of. In the vast majority of cases those will be identical, but not necessarily. I feel like there has to be a better solution out there.

0

精彩评论

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

关注公众号