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';
            $s->bindValue(':playername', $name);
            $s->bindValue(':teamid', $team);
            $s->bindValue(':position', $position);
            $s->bindValue(':jersey', $number);

        } catch (PDOEXception $e) {

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



    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


$player = new Player($pdo);

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

echo $player->get_player(1)->playername;
echo $player->get_player(1)->position;
  • \$\begingroup\$ Regarding OOP designs you might be interested to read this article about table mapping techniques. And no, your class isn't considered good object oriented design. \$\endgroup\$ Dec 25, 2015 at 11:32
  • \$\begingroup\$ Don't forget to prepare all your statements, including $id! \$\endgroup\$
    – Alex L
    Dec 27, 2015 at 0:31

2 Answers 2


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).


  • 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).
  • \$\begingroup\$ thanks for the feedback just started learning OOP in php so i was curious \$\endgroup\$ Dec 25, 2015 at 11:41

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.

  • \$\begingroup\$ Thank you for your reply, I am noticing that I have a lot to learn now that im seeing this word Mapping alot. Ill keep reading into it until i grasp whats going on. @πάντα ῥεῖ \$\endgroup\$ Dec 25, 2015 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.