sourcy Posted January 3, 2013 Share Posted January 3, 2013 (edited) Okay, please don't tell me I can do this with regular PHP and I'm making things more complicated on myself for doing it with OOP etc.. I know. I'm just doing this to learn OOP (and it could be used for more than just a login, I still would have a problem doing it) First I have this login page, Where I am creating a connection to the DB and then creating a new user. Then I call the function $user->login(); http://pastie.org/5618540 Database parent class http://pastie.org/5618537 User Child class http://pastie.org/5618533 When I have it written like that I get: "Fatal error: Call to a member function query() on a non-object in /home/sourcy/thesourcy.com/classes/User.php on line 15" So I changed $db-query("SELECT FROM") to Database::query() in the user child class. That got rid of that error, but then it will throw me this error: "Warning: mysql_query() expects parameter 2 to be resource, null given in /home/user/site.com/classes/Database.php on line 19 Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /home/user/site.com/classes/Database.php on line 20" Which essentially means that $this->$_link isn't getting set (since it's null). What am I missing? / Doing wrong? I'm new to OOP. And no, this code will not be going live. I understand the security vulnerability, I'm just doing it for practice/learning Also: If I use $db->query() on "login.php" it will work, but when I move it to the User class and try to use it, it doesn't work Edited January 3, 2013 by sourcy Quote Link to comment Share on other sites More sharing options...
sourcy Posted January 3, 2013 Author Share Posted January 3, 2013 I see that if I change the user construct to: " public function __construct($user, $pass) { $this->_user = $user; $this->_pass = $pass; Database::__construct('mysitedb', 'root', 'password', 'mydb'); }" it get's ride of the error then I just can use Database::query(); Is this how it's done? Quote Link to comment Share on other sites More sharing options...
trq Posted January 4, 2013 Share Posted January 4, 2013 The biggest issue: A User is not a type of Database, it therefore should not extend it. You should be passing your database object into the User object so that it can use it. Quote Link to comment Share on other sites More sharing options...
TOA Posted January 4, 2013 Share Posted January 4, 2013 I think your errors are stemming from here. Not your technical errors, but your logic errors (although probably both). First I have this login page, Where I am creating a connection to the DB and then creating a new user. Then I call the function $user->login(); The user should not log itself in. Query the db with the user input and if successful, construct a user/set a cookie/etc. This could be done procedurally, or you could make some sort of Authorization class. This is evident by the need to extend the DB class as a User. As trq noted: is a user a database? No. So it should not extend it; nor have any knowledge of it really. Just my opinion. Hope it helps Quote Link to comment Share on other sites More sharing options...
sourcy Posted January 4, 2013 Author Share Posted January 4, 2013 (edited) The biggest issue: A User is not a type of Database, it therefore should not extend it. You should be passing your database object into the User object so that it can use it. Ahh makes sense. I'm a noob at this OOP stuff as you can tell. So basically I would have to get rid of the "extends database" Then on login.php I would have to make it " $db = new Database('mysitedb', 'root', 'password', 'mydb'); $user = new User($_POST['user'], $_POST['pass'], $db); " ? But then how in the user class could I call the function "query" from the database class? Query the db with the user input and if successful, construct a user/set a cookie/etc. This could be done procedurally, or you could make some sort of Authorization class. This is evident by the need to extend the DB class as a User. As trq noted: is a user a database? No. So it should not extend it; nor have any knowledge of it really. Just my opinion. Hope it helps Ahh yeah, makes sense as well. I'm glad I posted this, instead of writing a bunch of code just to re-write this. Edited January 4, 2013 by sourcy Quote Link to comment Share on other sites More sharing options...
TOA Posted January 4, 2013 Share Posted January 4, 2013 I'm glad I posted this, instead of writing a bunch of code just to re-write this. Trial and error is a great learning method really Quote Link to comment Share on other sites More sharing options...
trq Posted January 4, 2013 Share Posted January 4, 2013 But then how in the user class could I call the function "query" from the database class? Assign it to a property: public function __construct(Database $db) { $this->db = $db; } You can then access it within the User class using $this->db. So, to execute query() you would use $this->db->query(). Quote Link to comment Share on other sites More sharing options...
sourcy Posted January 4, 2013 Author Share Posted January 4, 2013 (edited) Trial and error is a great learning method really Well if anyone cares to look, using both suggestions I now have this code: http://pastie.org/5618835 (yes I realize username, pass and DB were all left in, they've all been edited and changed) I think that's what was intended by your suggestions, and as such, I have altered my code. (which seems to work) Assign it to a property: public function __construct($user, $pass, Database $db) { $this->user = $user; $this->pass = $pass; $this->db = $db; } You can then access it within the User class using $this->db. So, to execute query() you would use $this->db->query(). Thanks, I just figured that out as you posted! Edited January 4, 2013 by sourcy Quote Link to comment Share on other sites More sharing options...
trq Posted January 4, 2013 Share Posted January 4, 2013 Well if anyone cares to look, using both suggestions I now have this code: http://pastie.org/5618835 I think that's what was intended by your suggestions, and as such, I have altered my code. (which seems to work) Thanks, I just figured that out as you posted! Getting closer. I would get rid of all of those static calls to Database. They are making your user class tightly coupled with some Database class. Also, you should implement type hinting like in my example. You really should define a Database interface though, and type hint to that interface. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.