MasterACE14 Posted August 14, 2011 Share Posted August 14, 2011 Good Day, I have a class that includes the page requested, but I need to pass the instance of several different classes to this 'loadbackend' class, so the page that the class includes has access to these instances. Either that, or bring the included page out of the scope of the class it's included in. I have things set up like this(relevant code only): index.php: # 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); /* page is included within this class, either bring that page to the scope of index.php, or pass all the classes above into this class */ 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; } } Thanks in advance! Kind Regards, Ace Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 14, 2011 Share Posted August 14, 2011 Which classes instances are you passing to LoadBackend? Because I can recommend a few things based on what you're doing.. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted August 14, 2011 Author Share Posted August 14, 2011 virtually all classes I have in my index.php script above. I was looking into Dependency Injection, but wasn't sure if maybe there's a better way of going about this? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 14, 2011 Share Posted August 14, 2011 You can use an ABSTRACT class which can force every single other class to use the LOADBACKEND's constructor, or however you go about handling the instances. Object Oriented needs to work like a machine, it has to be all connected to each other, so make use of design patterns and paradigm., Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted August 14, 2011 Author Share Posted August 14, 2011 You can use an ABSTRACT class which can force every single other class to use the LOADBACKEND's constructor, or however you go about handling the instances. But then the classes can't use their own constructor? If I have this... index.php: # 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); if(!isset($location)) $location = 'controllers/backend/'; else $location = $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); } Taking the method out of the loadbackend class, and just placing that code in index.php, everything works fine(being in the same scope). For the sake of cleaner, more readable and more maintainable code I've made the loadbackend class for this(as this will be used on several sites instead of just the one, I don't want to hard code the class instances being passed to loadbackend) There must be an easier way to do this? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 14, 2011 Share Posted August 14, 2011 LoadBackend is the base class, so all of your classes need to go through this classe's method anyways, You don't initiate LoadBackend, just initiate your classes normally and loadbackend will do the rest. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 14, 2011 Share Posted August 14, 2011 edit: Nvm this. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted August 14, 2011 Author Share Posted August 14, 2011 lol wait wait.... forget this part is in there... Database $db My issue is with scope correct? so virtually I want to do this... public function __construct($location, Database $db, $session, $online) { but with different websites, they will need more or less classes passed to LoadBackend as every website has different requirements. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 14, 2011 Share Posted August 14, 2011 I see, I thought you were just looking for a more efficient way of doing this, the first code should work no problem. $lb = new LoadBackend($db, PATH_CG.PATH_BACKENDS); /* page is included within this class, either bring that page to the scope of index.php, or pass all the classes above into this class */ But yes your correct. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted August 14, 2011 Author Share Posted August 14, 2011 I'm going to run with this for now until I come up with something better... # 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); 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); } Thanks again for your help! Cheers. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 14, 2011 Share Posted August 14, 2011 Have you figured it out? Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted August 14, 2011 Author Share Posted August 14, 2011 nope, I've tried explaining it again in a new post Lol, think I may have overcomplicated it here. http://www.phpfreaks.com/forums/index.php?topic=341443.0 Quote Link to comment 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.