Jump to content
Larry Ullman's Book Forums

Recommended Posts

I am a OOP-newbie - and I like this book a lot :-)

 

None of the the two classes in the chapter, User and Page, have a $pdo in the constructer. This means that all db-handling(select, insert) is in the controller-pages and not in the classes. There could be a getUserName-method in the User-class, but no.

 

I found another userclass on the internet where the $pdo is in the constructor, and then I can make a getUserName-method. BUT - if I then want to make some of the db-handling in the controller-page and not inside the class, the $pdo is required in the constructor, if I want to fetch data into my object. So I can't do that?

Error: Missing argument 1 for User::__construct()

 

It seems like that if I have the $pdo in the constructor, I MUST do all db-handling inside the class. And if I dont have the $pdo in the constructor I MUST do all db-handling outside the class(in the controller page).

 

My question is: when should I have the $pdo in my constructor?

 

Kim K

Share this post


Link to post
Share on other sites

There's no definite answer here. It pretty much depends on what you need to do with the class. There's a principle called the "Single responsibility principle" I really like. It says that a class and each method should only have one responsibility. This can often be a bit tricky to define, but "representing a User instance" and "creating a User instance" is so different they should probably be handled by different classes... So, how can do we do that?

 

1. Create a model:

Create a new class that takes the PDO object in the constructor and add methods for operations such a CRUD. On Select-type operations, build a User object and return it. The class will resemble the typical "Model" classes you find in frameworks. This is (often) my preferred way to handle this.

class User
{
    // As normal
}

class UserModel
{

    private $pdo;

    public function __constructor ( PDO $pdo )
    {
        $this->pdo = $pdo;
    }

    public function fetchById( $userId )
    {
        $result = // Perform PDO query and fetch to result:
      
        return new User( $result->username, .... ); // Create object
    }

}

2. Use a Factory:

Create a factory method or a class. If all you need to do with an object is creating it (as is often the case) a factory method can be suitable:

class User 
{

    // vars

    private function __construct( $username, $realName, $accessLevel )
    {
        $this->username = $username;
        ....
    }
    
    public static function createById( PDO $pdo, $userId )
    {
        // use PDO to fetch user into $row....
        return new self($row->userName, $row->realName, $row->accessLevel);
    }
}
  • Upvote 1

Share this post


Link to post
Share on other sites

Thanks Antonio

 

Both are good solutions, I think I like number 2 most, because there's only one class, and then I can bring the $pdo in only in some methods as an argument - where I need it.

Share this post


Link to post
Share on other sites

There are sure benefits to both methods. You can of course also move methods into a new class if they start to grow a lot in number. You'd then simply make the constructor public (You can do that anyway if you prefer) and use the class name instead of self.

 

Glad I could help out.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...