kratsg Posted October 26, 2007 Share Posted October 26, 2007 Let's say I have some sort of a user login management website of sorts. General idea would be different pages, profiles, login, logout, register, etc... So a user logs in, I call a class to validate this user. I use another class (class User) to fetch all the data for that particular user, set it into a mega array such as $user["id"] or $user["email"] to store all data to be called later. Page1.php include "someuserclass.php" $user = new User; $user->setuser($username); $user->compile_array();//sets $user to contain the array that is returned by the class function //from this point, I can easily call data, etc... Now, wait. Let's say I go to Page2.php, does this mean I have to re-initiate the class, do the same thing as I did for Page1.php (with the include, initiate class, setuser, and then compile data)? Is there some way to maintain the class over different pages (as you would do with cookies or sessions, without actually using cookies or sessions)? The only way that I can think of doing this is to include this in the "head.php" page which contains the header stuff, like top of the layout, title, etc... like so: Head.php include "someuserclass.php" include "somerequiredfunctions.php"; include "database_conn.php"; $user = new User; $user->setuser($username); $user->compile_array();//sets $user to contain the array that is returned by the class function //insert some html here Page1.php include "Head.php"; //do other stuff here, etc.. Page2.php include "Head.php"; //do some other stuff here... My biggest worry is that since I'm fetching data from the database, that this makes a NEW call to the database for each page that I load (to get user data). Is there anyway to maximize efficiency, and cut-down on server usage/memory? [hopefully, something that may include a mysql_free_result() function, which I don't know WHEN to use it, or why you would use it if it clears your variables...] Quote Link to comment https://forums.phpfreaks.com/topic/74820-maintaining-a-class-not-sure-how-to-ask-the-question-o_o/ Share on other sites More sharing options...
subcool Posted October 26, 2007 Share Posted October 26, 2007 afaik you have to either use sessions/cookies or call that method every time. Also you should use 'require_once' and 'include_once' to avoid declaring methods twice. And on the MySQL thing you'll just have to close the connection each time when you're done with it, although PHP closes those by itself after script execution is done. Quote Link to comment https://forums.phpfreaks.com/topic/74820-maintaining-a-class-not-sure-how-to-ask-the-question-o_o/#findComment-378486 Share on other sites More sharing options...
kratsg Posted October 26, 2007 Author Share Posted October 26, 2007 so a mysql_free_result combined with a mysql_close would help? Quote Link to comment https://forums.phpfreaks.com/topic/74820-maintaining-a-class-not-sure-how-to-ask-the-question-o_o/#findComment-378504 Share on other sites More sharing options...
subcool Posted October 26, 2007 Share Posted October 26, 2007 freeing the result will have no effect since if you close the database handle, the memory used for this is flushed. You should only free the result if you do a HUGE query and after that you want to do another (huge) query using the same database handle. Quote Link to comment https://forums.phpfreaks.com/topic/74820-maintaining-a-class-not-sure-how-to-ask-the-question-o_o/#findComment-378535 Share on other sites More sharing options...
kratsg Posted October 26, 2007 Author Share Posted October 26, 2007 I see... So a mysql_close will do what I need. For website integration and management with OOP, do you suggest having a class for the database, so that on each page, you include this class, run a function to open database connection, select database, and for queries? And then, just have multiple classes for user checking access, run the function to get user access and compare it to page access, etc...? Like: mysql class $q = new Mysql; $q->connect(); $q->select("some database"); @mysql_query("do some stuff blah blah"); Or would I be better off with: $q = new Mysql; $q->connect(); $q->select("some database"); $q->update("table","column","new value","where this","equals this"); I kinda prefer the first one, as you're able to have more customizability, but in terms of efficiency, which would be preferred? Then for user checking: $user = new User; $access = new Access; $access->access = true; $access->user_access = $user->access; $access->check_access(); It seems a bit simple for me to do it this way, instead of if/else statements on so many pages. Does anyone think this would be the best method for checking access using classes (assuming perfected functions). Quote Link to comment https://forums.phpfreaks.com/topic/74820-maintaining-a-class-not-sure-how-to-ask-the-question-o_o/#findComment-378880 Share on other sites More sharing options...
Mastodont Posted October 27, 2007 Share Posted October 27, 2007 $q = new Mysql; $q->connect(); $q->select("some database"); Better $q = new Mysql; $q->connect("some database"); And your user checking is very, very bizarre. What is the meaning of class Access? I presume all functionality should be located in class User. $user = new User("id"); // or "nick" or whatever $user->CheckAccess(); // but it could be already done in constructor ... if ($user->WritingAllowed) ... Quote Link to comment https://forums.phpfreaks.com/topic/74820-maintaining-a-class-not-sure-how-to-ask-the-question-o_o/#findComment-379110 Share on other sites More sharing options...
kratsg Posted October 27, 2007 Author Share Posted October 27, 2007 $user = new User("id"); // or "nick" or whatever $user->CheckAccess(); // but it could be already done in constructor ... if ($user->WritingAllowed) ... Not quite sure how a constructor class is set-up (I know its something about running once the class is called). How do you pass the "id" variable into the class? I was gonna have a method of accessing the database to get the filename of the script the user is access, check it's access level against the user's access level inside the class (let me know if this is the best option). Quote Link to comment https://forums.phpfreaks.com/topic/74820-maintaining-a-class-not-sure-how-to-ask-the-question-o_o/#findComment-379118 Share on other sites More sharing options...
Mastodont Posted October 27, 2007 Share Posted October 27, 2007 Huh, it should be of course $user = new User($id); if this is the best option Well, it depends on global architecture of your application. You can include script in independent files, you can have "nice URLs" and dispatch actions internally from URL parts ... there is no best option. IMHO. Quote Link to comment https://forums.phpfreaks.com/topic/74820-maintaining-a-class-not-sure-how-to-ask-the-question-o_o/#findComment-379133 Share on other sites More sharing options...
Jenk Posted October 29, 2007 Share Posted October 29, 2007 freeing the result will have no effect since if you close the database handle, the memory used for this is flushed. You should only free the result if you do a HUGE query and after that you want to do another (huge) query using the same database handle. That is misleading information. Semantically, you should free the result set when you have finished with it. It is not essential, however. P.S. if my site has over 1000 hits an hour, there will be a noticeable increase in performance when the memory is freed from every result set as soon as possible, compared to leaving the Zend engine to do it for me. Quote Link to comment https://forums.phpfreaks.com/topic/74820-maintaining-a-class-not-sure-how-to-ask-the-question-o_o/#findComment-380345 Share on other sites More sharing options...
aschk Posted October 31, 2007 Share Posted October 31, 2007 Constructors are done inside the class and get executed upon "construction" of the class. i.e. $class = new myClass(); calls the constructor of the class. The code for such is : class myClass { // This is the constructor. public function __construct(){ } // This is the constructor for <= PHP4 public function myClass() { } } If you want backward compatibility have the myClass() constructor called by the __construct() constructor, and put all your construction code inside the myClass() function. Quote Link to comment https://forums.phpfreaks.com/topic/74820-maintaining-a-class-not-sure-how-to-ask-the-question-o_o/#findComment-382050 Share on other sites More sharing options...
eZe616 Posted November 21, 2007 Share Posted November 21, 2007 $q = new Mysql; $q->connect(); $q->select("some database"); I have a quick question. I have a Database Class like the OP. Now I initialize the class in the class file itself. My class file looks kinda like this Class MySQL { // Necessary code goes here } $db = new MySQL(); Now is this a bad practice? I got it from a tutorial that did it this way and I thought it won't be so bad to have the class initialized already so I can just use it immediately without have to write the $db = new MySQL(); In every page that uses it. Quote Link to comment https://forums.phpfreaks.com/topic/74820-maintaining-a-class-not-sure-how-to-ask-the-question-o_o/#findComment-395861 Share on other sites More sharing options...
trq Posted November 21, 2007 Share Posted November 21, 2007 The only bad thing about that approuch would be that any time you need to change host,username, pass or database you actually need to change the code within the class. A better option would be to have your class read from some external config file, this way you only need to change the config file and not your code. Quote Link to comment https://forums.phpfreaks.com/topic/74820-maintaining-a-class-not-sure-how-to-ask-the-question-o_o/#findComment-395897 Share on other sites More sharing options...
eZe616 Posted November 21, 2007 Share Posted November 21, 2007 The only bad thing about that approach would be that any time you need to change host,username, pass or database you actually need to change the code within the class. A better option would be to have your class read from some external config file, this way you only need to change the config file and not your code. Yeah..I have an config file that is included before the class where the information of host username etc include 'config'; Class MySQL { // Necessary code goes here } $db = new MySQL(); Quote Link to comment https://forums.phpfreaks.com/topic/74820-maintaining-a-class-not-sure-how-to-ask-the-question-o_o/#findComment-395901 Share on other sites More sharing options...
trq Posted November 21, 2007 Share Posted November 21, 2007 Well, I don't see anything with your implimentation. Quote Link to comment https://forums.phpfreaks.com/topic/74820-maintaining-a-class-not-sure-how-to-ask-the-question-o_o/#findComment-395904 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.