trace96 Posted March 2, 2013 Share Posted March 2, 2013 (edited) I'm not new to the PHP scene, I'm just not up to par with the language standards. I've recently stumbled upon a new style called OOP. Anyways I have a few issues with my current script. I plan on expanding it but started with a "simple" script. My issues: 1. I'm unable to use $this->username in functions like "check_password" ideally I'd like to have it: function check_password() and it'd do $this->username, $this->password. The username/password would be set in the code (like below) but when attempted it just displays null. I've read online that I have to use magic functions as in __get or __set. I also want this to be as dynamic as possible so setVar($var) { $this->username = 'username'; } would not be feasible either. 2. In the function info() I want it to return whatever the request variable is .. right now it's only returning username. I'd assume it'd be something like $row->[$request] Any help / links to any resources would be highly appreciated. Feel free to suggest alternatives to this current code as I'm still not sure this is the best way to go about things. <? $mysqli = new mysqli("localhost", "username", "password", "dbname"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } function query($q){ global $mysqli; if(!$mysqli->query($q)){ die($mysqli->error); } return $mysqli->query($q); } class Database { public static function close(){ global $mysqli; $mysqli->close; } } class Auth { public $username; public $password; public $session_id; public function check_password($username, $password) { $result = query("SELECT * FROM `users` WHERE `username`='$username' AND `password`='$password'"); if($result->num_rows > 0){ return true; } return false; } private function check_session($username, $session_id) { $result = query("SELECT * FROM `users` WHERE `username`='$username' AND `session_id`='$session_id'"); if($result->num_rows > 0){ return true; } return false; } public function logged_in($username, $session_id) { return $this->check_session($username, $session_id); } public function info($request, $username) { $result = query("SELECT $request FROM `users` WHERE `username`='$username'"); if ($result->num_rows > 0){ $row = $result->fetch_object(); return $row->username; } return 'not found'; $result->close; } } class User extends Auth { public function get($result, $username) { if($this->logged_in($username, $session_id)){ return $this->info($result, $username); } return null; } } $auth = new Auth(); $auth->username = 'username'; $auth->password = 'password'; $auth->session_id = '55'; $user = new User(); if($auth->check_password($auth->username, $auth->password)){ echo 'Welcome: '.$user->get('username', $auth->username, $auth->session_id); }else{ echo 'Incorrect Password'; } Database::close(); ?> Edited March 2, 2013 by trace96 Quote Link to comment https://forums.phpfreaks.com/topic/275104-new-to-oop/ Share on other sites More sharing options...
denno020 Posted March 2, 2013 Share Posted March 2, 2013 (edited) The only reason you would need the magic getter or setter functions is if you're trying to access private variables from another class. Also, seeing as though class User extends class Auth, then instead of creating a new Auth, just create a new User. You will have access to the variables username, password etc because they are inherited to the User class from the Auth class. Hopefully that makes sense.. Denno Edited March 2, 2013 by denno020 Quote Link to comment https://forums.phpfreaks.com/topic/275104-new-to-oop/#findComment-1415934 Share on other sites More sharing options...
trq Posted March 2, 2013 Share Posted March 2, 2013 None of your code is OOP. For now, ignore that you have even seen it mentioned, and carry on as you were. Quote Link to comment https://forums.phpfreaks.com/topic/275104-new-to-oop/#findComment-1415973 Share on other sites More sharing options...
Hall of Famer Posted March 2, 2013 Share Posted March 2, 2013 Id say its better to use reflection than magic methods, but anyway its never a good practice to access private fields in another class. You should do it only when you have a very good reason, And I dont quite understand the logic of User class extending Auth. I'd normally assume that Auth is a sub-category of User, as a guest, bot and banned user are all technically users but auth is for admins, mods and other staff members? Its not a big deal in early design stage, but you need to have a clear logic. OOP is more than just using object oriented code, it requires object oriented way of thinking and design. But for now I wouldnt say you are doing a bad job, people just have to start from somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/275104-new-to-oop/#findComment-1415980 Share on other sites More sharing options...
AyKay47 Posted March 2, 2013 Share Posted March 2, 2013 I suggest studying OOP for quite some time before attempting to implement it into your code. You can find quite a few online and book resources on the subject. In short, the above code is not OOP whatsoever. Throwing some code into classes is not what OOP is about. Quote Link to comment https://forums.phpfreaks.com/topic/275104-new-to-oop/#findComment-1416068 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.