Kim K Posted February 5, 2014 Share Posted February 5, 2014 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 More sharing options...
Antonio Conte Posted February 5, 2014 Share Posted February 5, 2014 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); } } 1 Link to comment Share on other sites More sharing options...
Kim K Posted February 6, 2014 Author Share Posted February 6, 2014 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. Link to comment Share on other sites More sharing options...
Antonio Conte Posted February 6, 2014 Share Posted February 6, 2014 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. Link to comment Share on other sites More sharing options...
Larry Posted February 7, 2014 Share Posted February 7, 2014 Thanks, Antonio, and thanks to Kim for the nice words on the book! Link to comment Share on other sites More sharing options...
Recommended Posts