Perad Posted January 13, 2010 Share Posted January 13, 2010 Hi, I am trying to write a small MVC framework. I am having a little trouble setting something up. I want to have a single instance for the entire application. For example. The instance would contain the following. $this->db $this->session $this->validation These should be accessible to all classes should they try to access them. I have heard about magic __get() __set() methods but don't quite understand how they work or if they would help. Does anyone know how I can achieve this? If __get() __set() methods can be used could someone briefly explain how. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/188364-php5-creating-a-single-instance-for-an-entire-application/ Share on other sites More sharing options...
ignace Posted January 13, 2010 Share Posted January 13, 2010 Take a look at the FrontController pattern http://martinfowler.com/eaaCatalog/frontController.html Quote Link to comment https://forums.phpfreaks.com/topic/188364-php5-creating-a-single-instance-for-an-entire-application/#findComment-994395 Share on other sites More sharing options...
Perad Posted January 13, 2010 Author Share Posted January 13, 2010 Thanks, that sounds like it would work. So am I right in thinking that I would do something like this... class instance { } Then something like this. class controller { public function __construct($instance) { $this->i = $instance; } } class c extends controller { public function __construct() { $this->i->db = $this->load->library('db'); $this->i->auth = $this->load->library('db'); $this->i->session = $this->load->library('db'); } } This actually wouldn't require much modification to adapt what I have at the moment. Have I interpreted this correctly? Quote Link to comment https://forums.phpfreaks.com/topic/188364-php5-creating-a-single-instance-for-an-entire-application/#findComment-994404 Share on other sites More sharing options...
ignace Posted January 13, 2010 Share Posted January 13, 2010 Everything you specify in your controller (db, auth, session) is actually just a model and these may be present in the controller. I actually do not recommend adding your db directly into your controllers as your controller does not need to know that your application uses a database nor that it uses sessions. You can hide these behind a new model layer for example: $user = UserRepository::findByUsername(..); Uses a database to find the username but it actually does not know if this database is mysql, a flat file or sqlite or something.. Same applies for auth: AuthService::logon($this->getRequest()); Quote Link to comment https://forums.phpfreaks.com/topic/188364-php5-creating-a-single-instance-for-an-entire-application/#findComment-994449 Share on other sites More sharing options...
Perad Posted January 13, 2010 Author Share Posted January 13, 2010 I know about the controller and db's. I was just using db as an example as I couldn't come up with other library names. Quote Link to comment https://forums.phpfreaks.com/topic/188364-php5-creating-a-single-instance-for-an-entire-application/#findComment-994456 Share on other sites More sharing options...
ignace Posted January 14, 2010 Share Posted January 14, 2010 UserRepository and AuthService are obviously not controllers but models. Quote Link to comment https://forums.phpfreaks.com/topic/188364-php5-creating-a-single-instance-for-an-entire-application/#findComment-994832 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.