stuartmarsh Posted August 25, 2008 Share Posted August 25, 2008 I'm trying to create a small H-MVC framework and I am hitting a snag. This is how I want it to work: All controllers extend a base class You can load additional controllers from within an already called controller (H-MVC), and call them like $this->controller I'm using a singleton to add classes to already loaded controllers, but I hit an error when I try to use a function from a loaded controller. Here is the code. Core.php is loaded first. Core.php: <?PHP define(EXT, '.php'); function & load_library($library, $params = array(), $instantiate = TRUE) { static $libraries = array(); if( !isset($libraries[$library]) ) { if( $instantiate == TRUE ) { $libraries[$library] =& new $library($params); } } return $libraries[$library]; } class Load { function & controller($Controller, $Params = array(), $load = TRUE) { static $Controllers = array(); require_once($Controller.EXT); $Controllers[$Controller] =& new $Controller($Params); $base =& get_instance(); $base->$Controller =& $Controllers[$Controller]; return $Controllers[$Controller]; } } $Load =& load_library('Load'); $Load->controller('Controller', array(), FALSE); $controller =& $Load->controller('welcome', array(), FALSE); $controller->index(); ?> Controller.php: <?PHP class Controller { private static $instance; function __construct() { self::$instance =& $this; $this->load =& load_library('load'); } public static function &get_instance() { return self::$instance; } } function &get_instance() { return Controller::get_instance(); } ?> welcome.php <?PHP class Welcome extends Controller { function __construct() { parent::__construct(); } function index() { $this->load->controller('Cont2'); $this->Cont2->admin(); // THIS IS WHERE IT ERRORS } } ?> cont2.php: <?PHP class Cont2 extends Controller { function __construct() { parent::__construct(); } function admin() { echo "This is a sub controller<br />"; } } ?> Can anybody see why I am hitting an error when trying to call the admin function from within the welcome controller? Thanks, Stu Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/ Share on other sites More sharing options...
aschk Posted August 26, 2008 Share Posted August 26, 2008 You might want to create some public/protected/private variables inside some of your classes... You're calling $this->load , which doesn't actually exist (as far as I can see). You also have some naming inconsistencies, you call the classes things like 'Welcome' and 'Load' and yet you store their names in arrays in lower case like 'welcome' and 'load' ... which is it? It all looks a little crazy to me, can you explain in plain text what you're trying to achieve? Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-625932 Share on other sites More sharing options...
stuartmarsh Posted August 29, 2008 Author Share Posted August 29, 2008 This is what I am trying to achieve: I have a parent class called Controller There are several other classes that extend from Controller I want to be able to add new functions/variables to parent and have them pass down to all the children classes Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-628797 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 You might want to create some public/protected/private variables inside some of your classes... You're calling $this->load , which doesn't actually exist (as far as I can see). You also have some naming inconsistencies, you call the classes things like 'Welcome' and 'Load' and yet you store their names in arrays in lower case like 'welcome' and 'load' ... which is it? It all looks a little crazy to me, can you explain in plain text what you're trying to achieve? Very good point with the $this->load. I see where he tries to load it, but it's not actually set as a class property. Add a declaration for it. Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-628805 Share on other sites More sharing options...
ionik Posted August 31, 2008 Share Posted August 31, 2008 Your first file will not work correctly and should trigger a error ( you are trying to load the class with the incorrect naming ) welcome and Welcome will be read as two different things. The controller class should be setup as a abstract class function __construct() { parent::__construct(); } there is no need to call this again in a second class that is a child of your controller as it is already there. when you are trying to load the Cont2 class you are calling $this->load it need to be $this->Load->controller(). And for your instance var try changing your code for the get_instance() function to the following protected static $instance = __CLASS_NAME__; function get_instance() { return is_object(self::$instance) ? self::$instance : (self::$instance = new self::$instance()); } and try running this hope this helps GL With OOP~~ Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-630295 Share on other sites More sharing options...
ionik Posted August 31, 2008 Share Posted August 31, 2008 could not edit my post.... here is a better example I whipped up to show you. <?php class SessionAbstractInit { protected static $instances = __CLASS__; public function Parse_All() { echo 'Parse All'; } public function getInstance() { return (is_object($instances)) ? self::$instances : (self::$instances = new self::$instances); } } abstract class SessionAbstract extends SessionAbstractInit { protected static $Init; public static $method; public function __construct() { self::$Init = SessionAbstractInit::getInstance(); // This will hold the objects of $Init } public function print_stuff() { } } class Session extends SessionAbstract { protected static $instance = __CLASS__; public function getInstance() { return (is_object($instance)) ? self::$instance : (self::$instance = new self::$instance); } public function donothing() { echo '<br /> Do Nothing!!'; } } class SessionManagement { public function __construct() { // create instance of session $this->session = Session::getInstance(); } public function show() { $this->session->Parse_All(); $this->session->donothing(); } } $session = new SessionManagement(); $session->session->Parse_All(); $session->session->donothing(); $session->show(); ?> as you can see i created the abstract session which didn't do anything but in turn could load the class objects and turn them into a registry, and then branched two child classes off of it Session And SessionManagement. SessionManagement loaded itself contained its getinsance and a donothing function, Session Loaded everything together into one object $this->session which held the values for all the parent classes before it. You can see from this that you could easily modify in a loader function that will automatically load all classes into a single object in which you are trying to do Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-630300 Share on other sites More sharing options...
stuffradio Posted September 5, 2008 Share Posted September 5, 2008 If you want to know what he's trying to do, he's trying to copy the structure of CodeIgniter. (I know because I use it) Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-634359 Share on other sites More sharing options...
stuartmarsh Posted September 5, 2008 Author Share Posted September 5, 2008 If you want to know what he's trying to do, he's trying to copy the structure of CodeIgniter. (I know because I use it) Thats exactly right. I'm trying to create a framework that works the same way as CodeIgniter. However unlike CodeIgniter, I want to be able to call controllers from within controllers. Something that seems to be frowned upon on the CodeIgniter forums. I also currently use CodeIgniter btw. Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-634658 Share on other sites More sharing options...
LemonInflux Posted September 7, 2008 Share Posted September 7, 2008 have something like this as a base controller... class Controller { private $instances; function __get($key) { return $this->instances[$key]; } function __set($key, $value) { $this->instances[$key] = $value; } } Then you can use something like this... $this->cont2 = new cont2; $this->cont2->admin(); Or, create another function specifically for using the __set() or doing the same thing. ---------------- Now playing: Burden of a Day - For Tomorrow We Die via FoxyTunes Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-635765 Share on other sites More sharing options...
alexweber15 Posted September 11, 2008 Share Posted September 11, 2008 afaik instead of using all the references (which also afaik is redundant since objects are passed by reference anyways).... although this is a generic example and doesn't really apply to the specifics of your project, here's a good example of a singleton implementation: class Singleton { private static $instance = null; private function __construct(){} public static function getInstancia() { if (self::$instance == null) { self::$instance = new self(); } return self::$instance; } } hope this helps Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-638825 Share on other sites More sharing options...
stuartmarsh Posted September 11, 2008 Author Share Posted September 11, 2008 afaik instead of using all the references (which also afaik is redundant since objects are passed by reference anyways).... although this is a generic example and doesn't really apply to the specifics of your project, here's a good example of a singleton implementation: class Singleton { private static $instance = null; private function __construct(){} public static function getInstancia() { if (self::$instance == null) { self::$instance = new self(); } return self::$instance; } } hope this helps Hi alexweber15, Thanks for the suggestin but if you look at the controller.php it already uses this method. It fails to work when you extend the controller class more than once. I have been playing with LemonInfluxes code and it does the job nicely. Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-638900 Share on other sites More sharing options...
alexweber15 Posted September 11, 2008 Share Posted September 11, 2008 Hey, just to make sure we're talking about the same things here... when you say Controller.php do you mean Lemon's example or yours? Also, as a conceptual/generic side-note: Singletons should have a private constructor (or in your case maybe protected since you want to extend from it) and all the interaction to $instances should be done via a getInstance() function... but really, extending a Singleton is kind of um, contradictory in a way... Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-638945 Share on other sites More sharing options...
stuartmarsh Posted September 11, 2008 Author Share Posted September 11, 2008 Sorry should have stated which one. My original class used a singleton but I have since altered it to use __GET and __SET as per LemonInfluxs code. I have since found out that the concept I am trying to achieve is called an observer pattern and not a singleton. Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-638948 Share on other sites More sharing options...
alexweber15 Posted September 11, 2008 Share Posted September 11, 2008 good to know an observer is a bit (lot) more complicated... Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-638953 Share on other sites More sharing options...
stuartmarsh Posted September 11, 2008 Author Share Posted September 11, 2008 good to know an observer is a bit (lot) more complicated... lol. Just starting to investigate it. It sounds like you've got experience with it. Any hints and tips? Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-638956 Share on other sites More sharing options...
alexweber15 Posted September 11, 2008 Share Posted September 11, 2008 good to know an observer is a bit (lot) more complicated... lol. Just starting to investigate it. It sounds like you've got experience with it. Any hints and tips? to say ive got experience with it is a complete overstatement ive been studying design patterns intensely for the past few weeks and have received a lot of help from ppl here on the boards (and many others) and from professors at uni so ive got at least concepts and theoretical implementations down, kind of im at work right now so i gotta be kinda brief, but check these links out, they are really helpful! http://en.wikipedia.org/wiki/Observer_pattern http://www.fluffycat.com/PHP-Design-Patterns/Observer/ http://devzone.zend.com/article/5-PHP-Patterns-The-Observer-Pattern also there's a ton of published literature on the subject, my favorites being: - "The Gang of Four: Design Patterns, Elements of Reusable OO Software" - "Architect's guide to php design patterns" ill leave the details and ethics of how to obtain them up to you Link to comment https://forums.phpfreaks.com/topic/121285-mvc-framework-trying-to-add-to-singleton/#findComment-639020 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.