Kemik Posted August 12, 2007 Share Posted August 12, 2007 Hello, At the moment I have all my main functions in my Session class (session.php) but I want to split this file up in to different sections E.g. User Class, News Class, etc so it's more modular. If my session.php file contains: <?php include("database.php"); include("mailer.php"); include("form.php"); class Session { var $variables; function session() { $this->time = time(); $this->startSession(); } function startSession() { global $database; //The database connection session_start(); //Tell PHP to start the session /* Determine if user is logged in */ $this->logged_in = $this->checkLogin(); /** * Set guest value to users not logged in, and update * active guests table accordingly. */ if(!$this->logged_in){ $this->username = $_SESSION['username'] = GUEST_NAME; $this->userlevel = GUEST_LEVEL; $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time); } /* Update users last active timestamp */ else{ $database->addActiveUser($this->username, $this->time); } /* Remove inactive visitors from database */ $database->removeInactiveUsers(); $database->removeInactiveGuests(); /* Check if user is in a clan. If so, make clan ID */ $this->memofclan = $this->inClan(); /* Set referrer page */ if(isset($_SESSION['url'])){ $this->referrer = $_SESSION['url']; }else{ $this->referrer = "/"; } /* Set current url */ $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF']; } /** * checkLogin - Checks if the user has already previously * logged in, and a session with the user has already been * established. Also checks to see if user has been remembered. * If so, the database is queried to make sure of the user's * authenticity. Returns true if the user has logged in. */ function checkLogin(){ global $database; //The database connection /* Check if user has been remembered */ if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){ $this->username = $_SESSION['username'] = $_COOKIE['cookname']; $this->userid = $_SESSION['userid'] = $_COOKIE['cookid']; } /* Username and userid have been set and not guest */ if(isset($_SESSION['username']) && isset($_SESSION['userid']) && $_SESSION['username'] != GUEST_NAME){ /* Confirm that username and userid are valid */ if($database->confirmUserID($_SESSION['username'], $_SESSION['userid']) != 0){ /* Variables are incorrect, user not logged in */ unset($_SESSION['username']); unset($_SESSION['userid']); return false; } /* User is logged in, set class variables */ $this->userinfo = $database->getUserInfo($_SESSION['username']); $this->username = $this->userinfo['username']; $this->userid = $this->userinfo['userid']; $this->userlevel = $this->userinfo['userlevel']; $this->clanminfo = $database->getClanMemberInfo($_SESSION['username']); $this->clanid = $this->clanminfo['clanid']; $this->clanmlevel = $this->clanminfo['level']; $this->claninfo = $database->getClanInfo($this->clanid); return true; } /* User not logged in */ else{ return false; } } // rest of functions like login, inClan, logout, register, editnews, editprofile, etc } ?> In a new Class would I have to just include session.php before I start the class? I can just copy the functions in to a new class in user.php and then replace any $this-> to $session->. How would be best to global session? in every function or can I do it at the top of the class so it does it automatically in every function. As you can see, I'm still learning with classes but any advise would be appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/64493-splitting-a-class/ Share on other sites More sharing options...
Kemik Posted August 12, 2007 Author Share Posted August 12, 2007 Bump, If you need any more information to help me, let me know. Quote Link to comment https://forums.phpfreaks.com/topic/64493-splitting-a-class/#findComment-321723 Share on other sites More sharing options...
nloding Posted August 12, 2007 Share Posted August 12, 2007 It's hard, I'm trying to do the same thing. You need to separate them out by function -- look at what functions you have and group them together. Some oddballs always exist though. I'm working on my first OOP site and I'm basing stuff off the exact same tutorial you added to above (evolt's article, which I posted in a thread earlier today too). I'm adding to the whole thing and separating it out as follows: constants.php -- duh. database.php -- wrapper for all database functions, and contains commonly used functions, such as verifying username and password. security.php -- the class that actually performs the security checking, such as "checkLogin()" or "confirmUID()" session.php -- anything dealing with sessions -- so this includes setting the variables, starting the session, destroying the session on logout. What I did was just listed what functions I needed and group them together logically. I'm still learning though, so take all that with a grain of salt Quote Link to comment https://forums.phpfreaks.com/topic/64493-splitting-a-class/#findComment-321756 Share on other sites More sharing options...
Kemik Posted August 12, 2007 Author Share Posted August 12, 2007 I'm trying to split up my sessions.php page which is almost 1.5k lines now due to the huge amount of functions. As you can imagine, this is makes maintaining and tracking the functions hard. It's really hard to split because loads of things need variables from the sessions file. Quote Link to comment https://forums.phpfreaks.com/topic/64493-splitting-a-class/#findComment-321815 Share on other sites More sharing options...
keeB Posted August 12, 2007 Share Posted August 12, 2007 Take for example your checkLogin function. That's really probably a member of the user or authentication object. OO is really hard to teach. Abstraction is really hard to teach as well, you'll only understand through experience. OO is really about messaging. You have to keep 1 idea in mind. Don't ask an object for information, tell the object to do something for you. A deviation from this, which I often use to explain OO design, is to think of a light bulb. Procedurally, you would query the light bulb. Are you on? If not, turn on. Conversely, you can just tell the light to turn on, thus encapsulating the implementation of the ligh tbulb. Once you see OO in this way, you'll understand that for a function like checkLogin() where most of the data is user data, that the User class might be responsbile for this in your case. I would make an argument, however, that it's really the Authentication/Authorization part of your app which should handle this. I seem to have rambled a bit about these principles, sorry about that. I think before you can worry about your design choices, it's best to be educated/learning about OO design/patterns in general. I'll end with an attempt to solve your problem in a quick way. Conceptualize what each function in your Session class is really doing, and don't be afraid to pass objects in to function constructors. I could easily see a method like the following: <?php // apart of the user class public function checkLogin(Session $sess) { if ($this->authed || $sess->authed) return true; return false; } public function authenticate(DatabaseConnection $db, Session &$sess) { $this->authed = $db->doLogin($this); // returns true for logged in, false for logged out $sess->auth($this); } //apart of session object public function auth(User $user) { $this->authed = $user->name; // silly impl, but (i hope) you get the idea } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64493-splitting-a-class/#findComment-321857 Share on other sites More sharing options...
nloding Posted August 13, 2007 Share Posted August 13, 2007 I like the example and the light bulb analogy. That helped me ... I see a few more functions I can split into different places. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/64493-splitting-a-class/#findComment-321992 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.