1

I'm new to using the object oriented paradigm with PHP, so I may have missed something obvious.

I have a class Question, with multiple subclasses for different styles of question, instances of which can either be created from the MySQL database or created by the user in a form. However if it's created by the user it won't yet have any of the auto-incrementing IDs that are attached to it in the database.

What's the best way to go about this? I'm unfortunately somewhat stumped.

Should I use a different class for the user-created Question until it's been added to the database? Should I have them be the same class, but with different methods to instantiate them depending on the source? Something else?

5
  • Can you elaborate further what exactly you're stumped about? Commented Jul 6, 2014 at 10:14
  • @deceze Sorry, yes - I'll edit that in Commented Jul 6, 2014 at 10:15
  • Just don't make the ID mandatory. Commented Jul 6, 2014 at 10:15
  • Why dont you just create the new object in the database when the user submits the formÉ Or maybe I just dont get what your question is Commented Jul 6, 2014 at 10:15
  • @KyleK how would that work? have a separate class to handle user-added questions? Commented Jul 6, 2014 at 10:20

1 Answer 1

4

First of all, your Question classes should have nothing to do with the database in the first place. They should purely be dumb domain objects, which just hold data and maybe enforce a bit of data validation on it:

class Question {

    protected $title;
    protected $body;

    ...

    public function setTitle($title) {
        if (strlen($title) == 0) {
            throw new InvalidArgumentException(...);
        }
        $this->title = $title;
    }

    public function getTitle() {
        return $this->title;
    }

    ...

}

Depending on your requirements, you make some of these attributes required by requiring them in the constructor. By doing that you ensure that if you have an instance of Question, it has at least the minimal attributes that make it a valid question:

public function __construct($title) {
    $this->setTitle($title);
}

Again, notice that this object has absolutely no concept of what a database is. That's the responsibility of some other class, which has access to a database connector and can read from Question objects to store them in the database and create new Question objects from database data:

class QuestionStore {

    protected $db;

    public function __construct(PDO $db) {
        $this->db = $db;
    }

    public function getQuestion(...) {
        $data = $this->db->query('SELECT ...');

        $question = new Question($data['title']);
        $question->setBody($data['body']);
        return $question;
    }

}

(You can debate about separating those responsibilities further into factories etc, but you get the basic idea.)

How to handle ids then becomes pretty trivial. Either you just make the id one of the non-required attributes of your Question class, so some Question instances have ids and others don't while they haven't yet been saved in the database. Or, if it's somewhat important to your code to distinguish between those two types of questions more easily, make a subtype out of it:

class StoredQuestion extends Question {

    protected $id;

    public function __construct($id, $title) {
        $this->setId($id);
        parent::__construct($title);
    }

    ...

}
Sign up to request clarification or add additional context in comments.

2 Comments

OK, thanks. Somehow I'd not come across factories etc before - I'll do some more reading on design patterns then get stuck in. Anyone know any good online resources for such - either PHP-focussed or more general?
Thanks for answer. I am accustomed to connecting to db through procedural PHP and this is a big help and step in the right direction when working with OOP PHP/MySQL.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.