Shadowing Posted February 27, 2012 Share Posted February 27, 2012 Was wondering if anyone had any thoughts on how im running settings on my text base game. I currently have like 60 settings stored in the data base. These are only admin control settings. They set what things cost in the game and such. I was thinking today that would it be better instead of running a query on every page to grab the settings that page uses. I just simply do one huge select ALL query for the settings when a user logs in and turn all the setting into super global $_SESSION. Also this way any function i made i can simply use the SESSION instead of grabing settings out side the function or creating a query for it with in the function. So i was thinking doing this would extremly speed up my game wouldnt it? wondering if anyone has any thoughts on this Quote Link to comment https://forums.phpfreaks.com/topic/257864-some-advice-on-using-sessions/ Share on other sites More sharing options...
requinix Posted February 27, 2012 Share Posted February 27, 2012 Only if you're already using sessions for something else (which I expect you are). Also this way any function i made i can simply use the SESSION instead of grabing settings out side the function or creating a query for it with in the function. Abstract that out. Use a Registry for all the settings, then have your code use that Registry. Then your code won't care about where the settings actually come from. For the sake of making things easier, I'd forgo the usual pattern and just go with a bunch of static variables. class Settings { public static $SETTING1; public static $SETTING2; public static $SETTING3; private function __construct() {} public static function load() { if (!defined("SID")) session_start(); if (empty($_SESSION["app settings"])) { // load settings from file and into $_SESSION } $s = $_SESSION["app settings"]; // shorthand Settings::$SETTING1 = $s["setting1"]; Settings::$SETTING2 = $s["setting2"]; Settings::$SETTING3 = $s["setting3"]; } } Settings::load(); Then Settings::$SETTING1 to grab a setting. Since you're effectively caching the settings, I'd put in some mechanism for automatically reloading the settings in case they change. I suggest looking at the file modification time. Quote Link to comment https://forums.phpfreaks.com/topic/257864-some-advice-on-using-sessions/#findComment-1321634 Share on other sites More sharing options...
Shadowing Posted May 29, 2012 Author Share Posted May 29, 2012 Alright 2 months later and looking back over what you said lol really wish i understood this more Now i kinda know what class's are but i dont know what plubic static does and i notice this line where you are creating a blank function? private function __construct() {} Is using :: some sort of syntax for class's? oh boy Quote Link to comment https://forums.phpfreaks.com/topic/257864-some-advice-on-using-sessions/#findComment-1349511 Share on other sites More sharing options...
scootstah Posted May 29, 2012 Share Posted May 29, 2012 but i dont know what plubic static does Public static means that the visibility is "public", meaning you can access the object outside of the class itself, and the object is "static", which means you can access the object without having to instantiate the class. and i notice this line where you are creating a blank function? private function __construct() {} Since the class uses all static objects, there is no reason to instantiate it - setting the constructor to private means that it cannot be instantiated and will throw an error if you try. Is using :: some sort of syntax for class's? :: is the Scope Resolution Operator. It is essentially the equivalent to the -> operator for static objects. Quote Link to comment https://forums.phpfreaks.com/topic/257864-some-advice-on-using-sessions/#findComment-1349513 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.