Kim K
-
Posts
2 -
Joined
-
Last visited
Posts posted by Kim K
-
-
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
Ch 9 Pdo
in PHP Advanced and Object-Oriented Programming: Visual QuickPro Guide (3rd Edition)
Posted
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.