MasterACE14 Posted August 14, 2011 Share Posted August 14, 2011 I'm trying to get a handle on this... here is my index.php file... # Connect to Database $db = new MySQL('localhost', DB_USER, DB_PASS, DB_NAME); # Online Users $online = new Online($db); # User Session $session = new Session($db); # Load Backend $lb = new LoadBackend($db, PATH_CG.PATH_BACKENDS); here is my LoadBackend class for loading a selected page... <?php Class LoadBackend { private $db; private $location; public function __construct(Database $db, $location,) { $this->db = $db; $this->location = $location; $this->load_backend(); } private function load_backend() { global $content; if(!isset($this->location)) $location = 'controllers/backend/'; else $location = $this->location; if(isset($_GET['page'])) $_GET['page'] = $_GET['page']; else $_GET['page'] = 'home'; $_GET['page'] = htmlentities(trim(str_replace(chr(0), '', $_GET['page']))); // remove Null Bytes if(file_exists($location.$_GET['page'].EXT)) { require_once($location.$_GET['page'].EXT); } else { require_once($location.'home'.EXT); } return true; } } ?> now my problem is, because I am using 'require_once' to get the selected page within my LoadBackend class, these class instances can't be used by that required file... # Online Users $online = new Online($db); # User Session $session = new Session($db); now I could hard code them into my LoadBackend class by passing them as arguments like so... public function __construct(Database $db, $location, $session, $online) { but because I will be using this script on several different websites, not all of them will need to use these objects, and some will use different objects. So I don't want to 'hardcode' any objects that aren't 'required' like my database class. What is the best way to go about either putting these objects into the scope of LoadBackend or... bring the required file in LoadBackend into the scope of index.php, the file in which LoadBackend is called? Any help is appreciated! Thanks a million, Ace Quote Link to comment https://forums.phpfreaks.com/topic/244728-scope-issues/ Share on other sites More sharing options...
MasterACE14 Posted August 14, 2011 Author Share Posted August 14, 2011 maybe it's more achievable with just a loadbackend() function rather than a class? function load_backend($location) { global $content; if(!isset($location)) $location = 'controllers/backend/'; if(isset($_GET['page'])) $_GET['page'] = $_GET['page']; else $_GET['page'] = 'home'; $_GET['page'] = htmlentities(trim(str_replace(chr(0), '', $_GET['page']))); // remove Null Bytes if(file_exists($location.$_GET['page'].EXT)) { require_once($location.$_GET['page'].EXT); } else { require_once($location.'home'.EXT); } } Quote Link to comment https://forums.phpfreaks.com/topic/244728-scope-issues/#findComment-1257054 Share on other sites More sharing options...
trq Posted August 14, 2011 Share Posted August 14, 2011 my problem is, because I am using 'require_once' to get the selected page within my LoadBackend class, these class instances can't be used by that required file... I'm really not sure what you mean by this. Once a file has been required, it's functions / classes are available anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/244728-scope-issues/#findComment-1257086 Share on other sites More sharing options...
MasterACE14 Posted August 14, 2011 Author Share Posted August 14, 2011 the file that is required uses methods from these objects: # Connect to Database $db = new MySQL('localhost', DB_USER, DB_PASS, DB_NAME); # Online Users $online = new Online($db); # User Session $session = new Session($db); But I'm getting this notice: Notice: Undefined variable: session in \controllers\backend\login.php on line 7 Quote Link to comment https://forums.phpfreaks.com/topic/244728-scope-issues/#findComment-1257088 Share on other sites More sharing options...
trq Posted August 14, 2011 Share Posted August 14, 2011 Of course. The variable $session won't be in scope. You'll need to either pass them in, or have the code that depends upon the MySql, Online and Session objects to instantiate them itself. Quote Link to comment https://forums.phpfreaks.com/topic/244728-scope-issues/#findComment-1257090 Share on other sites More sharing options...
MasterACE14 Posted August 14, 2011 Author Share Posted August 14, 2011 I think I'm making it out to be a bigger issue than it really is. I'll do the former. Cheers, Ace Quote Link to comment https://forums.phpfreaks.com/topic/244728-scope-issues/#findComment-1257095 Share on other sites More sharing options...
MasterACE14 Posted August 14, 2011 Author Share Posted August 14, 2011 A friend has enlightened me as to what I was trying to do, can be done with the following 'registry' class: <?php class Registry{ private $registry = array(); public __set( $var, $val ) { $this->registry[$var] = $val; } public function __get( $var ) { return $registry[$var]; } } ?> For those who may be trying to achieve something similar. Quote Link to comment https://forums.phpfreaks.com/topic/244728-scope-issues/#findComment-1257147 Share on other sites More sharing options...
trq Posted August 15, 2011 Share Posted August 15, 2011 Yeah a registry will work. It's akin to using globals however. You should look into 'dependency injection'. Quote Link to comment https://forums.phpfreaks.com/topic/244728-scope-issues/#findComment-1257501 Share on other sites More sharing options...
MasterACE14 Posted August 15, 2011 Author Share Posted August 15, 2011 ok, will do. Back to the drawing board lol. Quote Link to comment https://forums.phpfreaks.com/topic/244728-scope-issues/#findComment-1257502 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.