lcaesars Posted January 31, 2008 Share Posted January 31, 2008 Hello everybdy, Dealing with OOP last couple of days, I learned a lot. But I guess, I'm still missing the OOP concept and the logic behind it. Simply, I can't decide to use classes or not. To make my understanding clear on OOP I would be greateful if you can help me on my example below. For example, we need authentication for a website. I defined AUTH_MODE variable in config file. It can have 3 values. "None","Simple" and "User". None is for websites we don't need private pages. Simple is for admin pages, where there is only one username and password stored in a config file. And User is for the systems there exist user registration with db interaction. #1 Up to now, is everything ok? What I want to ask here is, do I need seperate class files for this three modes? Let me continue with my example. I'm designing the User class now. It will have lots of methods like, login,confirm,lost_password,change_details.. And also there must be connection with my DB and Session Class.. #2 Should I put all the methods for User in the same class file or should I somehow split it into new files? #3 And finally, whats the best way to include DB and Session classes? I mean is there any way other than instantiating my DB class in Auth class? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/88696-simple-oop-help/ Share on other sites More sharing options...
Daniel0 Posted January 31, 2008 Share Posted January 31, 2008 1) You don't have to, but you could have an abstract class called Login and then have some sub-classes called Login_None, Login_Simple and Login_User. 2) I'd say one class per file. 3) You could use a registry, a singleton or you could pass the object as an argument to the method. Quote Link to comment https://forums.phpfreaks.com/topic/88696-simple-oop-help/#findComment-454310 Share on other sites More sharing options...
aschk Posted February 11, 2008 Share Posted February 11, 2008 par example: <?php /** * User class. * */ class user { private $username; private $password; public function __construct($username,$password){ $this->username = $username; $this->password = $password; } public function getUsername(){ return $this->username; } public function getPassword(){ return $this->password; } } /** * Abstract login class. * */ abstract class login { abstract public function auth(user $user); } /** * Does the lookup in the file. * */ class login_simple extends login { public function auth(user $user){ // open file if($user->getUsername() == $file['username'] && $user->getPassword() == $file['password']){ return true; } return false; } } /** * Does the lookup in the database. * */ class login_user extends login { public function auth(user $user){ // connect to database $db->query("SELECT id FROM users WHERE username='{$user->getUsername()}' AND password = '{$user->getPassword()}'"); if($db->numRows() === 1){ return true; } return false; } } /** * Does NO lookup, always authenticates. * */ class login_none extends login { public function auth(user $user){ // Always auths. return true; } } // Test run. $user = new user("joe","pass"); $login = new login_simple(); // Authenticated. if($login->auth($user)){ // do something here; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88696-simple-oop-help/#findComment-463937 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.