A class to add and select players within a database

Posted on

Problem

I have a question and want to know if my class is considered object oriented.

class Player {

    private $db

    function __construct($db) {

        $this->db = $db;

    }

    public function set_player($name, $position, $number, $team) {

        try {

            $sql = 'INSERT INTO players SET playername = :playername, position = :position, teamid = :teamid, jersey = :jersey';
            $this->db->prepare($sql);
            $s->bindValue(':playername', $name);
            $s->bindValue(':teamid', $team);
            $s->bindValue(':position', $position);
            $s->bindValue(':jersey', $number);
            $s->execute();

        } catch (PDOEXception $e) {

            echo 'Could not add player, please try again.';
            exit();

        }

    }

    public function get_player($id) {

        $result = $this->db->query('SELECT * FROM players WHERE id =' . $id);
        $row = $result->fetch(PDO::FETCH_OBJ);
        return $row;

    }

}

In another file I will use this class in this way

include_once('./includes/global.php');

$player = new Player($pdo);

$player->set_player(Chris, Forward, 13, 1);

echo $player->get_player(1)->playername;
echo $player->get_player(1)->position;

Solution

Calling get_player twice is not a good idea, as each time a db query is necessary.

As you are asking about OOP: You could add an actual Player object, which contains playername, position, etc as fields. Your current class could then be renamed PlayerDAO or something, and would then return a player object.

The main advantage would be that a concrete object is easier to use than an anonymous one (classes are to some degree self-documenting, while you can’t be sure what an anonymous object actually contains).

Misc

  • don’t die or echo in classes, it makes code less flexible and harder to reuse, as the calling code cannot try to recover.
  • get_player and set_player could be renamed to get and set as the class is already called player.
  • It would probably also be good to rename set to save or similar, as it makes more explicit that there is some kind of database interaction (set sounds as if a private field is set).

I have a question and want to know if my class is considered object oriented.

No, your class can’t be considered object oriented, because it violates some basic rules:

  • Data encapsulation
    Your Player class just operates on primitive data items, directly exposes raw database query results (row) respectively.

  • Encapsulation/separation of behaviour
    Your Player class accesses the database table directly, echoes error messages directly to the console and exits the program immediately on failure.

What you’ll actually need to make up a good object oriented design requires more than just a single class.

In your particular case it’s mainly about implementing an appropriate O/R mapping as described in this article.

In brief:

  • You should have a Player class that just encapsulates the data and provides operations that directly relate to the data it encapsulates (including getter/setter functions for the private data members).

  • You should have a separate PlayerTableMapper class, that provides all of the operations (CRUD == Create, Read, Update, Delete), that interact with the corresponding database table

As you can see from the article linked above, that’s a pretty broad topic, so excuse me if I’m stopping at this point and don’t replicate all of the points to obey in particular.

Leave a Reply

Your email address will not be published. Required fields are marked *