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

Link to comment
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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...