Stooney Posted October 3, 2008 Share Posted October 3, 2008 First off, sorry for the huge post, I kept it as small as I can without making it senseless (I hope). I've recently been reading up on MVC and am trying to work through it and understand it all better. I've never really used a registry on a website. Just passed variables around as needed. This may be a design flaw. I have a registry object which is to be used 'globally' by any other object that needs it. To make this work without globalizing it I do $object=new Object(&$registry) when I make a new object, and in the contructor it saved the registry object $this->registry=$registry. The idea is that all of my objects have access to the same registry object, rather than each having it's own version of the registry. Is there a better way to do this? My problem is I can't seem to use objects that are in the registry. I get these errors: Notice: Undefined property: Registry::$template in C:\xampp\htdocs\stooney\controllers\index.php on line 4 Fatal error: Call to a member function set() on a non-object in C:\xampp\htdocs\stooney\controllers\index.php on line 4 With this (template is an object stored in the registry, is the syntax wrong for this?): <?php class Controller_Index Extends Controller_Base{ function index(){ $this->registry->template->set('first_name', 'Dennis'); $this->registry->template->show('index'); } } ?> When doing a print_r on $registry while in Controller_Index (above) I see this which leads me to believe that I should be able to use $template's methods: Registry Object ( [vars:private] => Array ( [db_prefix] => stooney_ [db] => Db Object ( ) [template] => Template Object ( [registry:private] => Registry Object *RECURSION* [vars:private] => Array ( ) ) [router] => Router Object ( [registry:private] => Registry Object *RECURSION* [path:private] => C:\xampp\htdocs\stooney\\controllers\ [args:private] => Array ( ) ) ) ) Here's what it looks like: index.php [code] <?php $registry=new Registry(); //Database object $db=new Db(...); $registry->('db', $db); //Template object $template=new Template(&$registry); $registry->set('template', $template); //Router $router=new Router(&$registry); $router->setPath(site_path.'controllers); $registry->set('router'), $router); //etc ?> Example of a class <?php class Router{ private $registry; private $path; private $args=array(); function __construct($registry){ $this->registry=$registry; } //Cut out the methods ?> [/code] Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 3, 2008 Share Posted October 3, 2008 Just make the registry a singleton. Quote Link to comment Share on other sites More sharing options...
Stooney Posted October 3, 2008 Author Share Posted October 3, 2008 Haven't really worked with singletons, would you mind showing a quick example of one? I can figure it out from there. Quote Link to comment Share on other sites More sharing options...
corbin Posted October 3, 2008 Share Posted October 3, 2008 This is the basic gist of a singleton (one way to do it anyway): <?php class singleton { static private $isnt = null; public GetInst() { if($this->inst == null) $this->inst = new singleton; return $this->inst; } } $s = singleton::GetInst(); $s->Something(); singleton::GetInst()->Something(); Both of those would be operating on the same instance. Quote Link to comment Share on other sites More sharing options...
Stooney Posted October 4, 2008 Author Share Posted October 4, 2008 I can't seem to make it past this error: Fatal error: Using $this when not in object context in C:\xampp\htdocs\stooney\includes\classes\Singleton.php on line 6 Here's what the class looks like <?php class Singleton{ private static $instance=null; public function getInstance($class){ if($this->instance==null){ $this->instance=new $class; } return $this->instance; } } ?> And my usage: <?php $registry=Singleton::getInstance('registry'); $registry->set('somekey', 'thevalue'); ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 4, 2008 Share Posted October 4, 2008 That's because you can't use $this in static functions. *cough corbin cough* Just kidding. Anyway, use this class: <?php class Singleton { private static $instance; protected function __construct() { } protected function __clone() { } protected function __wakeup() { } public static function getInstance() { if (!(self::$instance instanceof self) { self::$instance = new self; } return self::$instance; } } I find that those other functions being added with blank declarations to be slightly more thorough, but it doesn't really matter. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 11, 2008 Share Posted October 11, 2008 Making __wakeup() protected won't accomplish anything. That's just the method that's called when the object is unserialized. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 11, 2008 Share Posted October 11, 2008 Making __wakeup() protected won't accomplish anything. That's just the method that's called when the object is unserialized. I meant to write __sleep(). >_< 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.