SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Non-Member
    Join Date
    Nov 2004
    Location
    Ithaca, NY
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Registry giving a frustrating error...

    I've put together a simple registry class based on the one on phppatterns.net and some feedback I've seen on this forum, and I'm getting an error that really baffles me.

    Here's the error:

    Fatal error: Using $this when not in object context in C:\Blahblahblah\Registry.php on line 16
    Here's the class. I've marked the line with the error:

    Code:
    <?
    
    class Registry {
    
    	var $_cache;
    
    	function Registry() {
    		$this->_cache = array();
    		}
    
    	function setEntry($key, &$item) {
    		$this->_cache[$key] = &$item;
    		}
    
    	function &getEntry($key) {
    		return $this->_cache[$key];  //ERROR ON THIS LINE (16)
    		}
    
    	function isEntry($key) {
    		return ($this->getEntry($key) !== null);
    		}
    		
    	function &getInstance() {
    		static $registry = array();
    		if (!$registry) {
    			$registry[0] =& new Registry();
    			}
    		return $registry[0];
    		}
    	}
    
    
    $reg =& Registry::getInstance();
    $reg->setEntry('database', new DBC());  // Database connection object
    
    ?>
    I'm not that great with PHP but I've tried everything I can think of and keep getting the same error... does anyone know why I'm getting it and how to fix it?

  2. #2
    Non-Member
    Join Date
    Nov 2004
    Location
    Ithaca, NY
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heh.

    Why is it I can spend hours trying to figure out an error, then post a message about it and figure it out on my own 5 minutes later?

    My problem was not in that file at all, but in the way I called one of the functions in another file using the registry...

    So disregard / delete if you like.

  3. #3
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Odd as it seems, that is not all that unusual of an experience. I think it is a bit of serendipity from organizing your thoughts to present a minimal example of your problem. Thinking through and writing up an explanation of your problem often puts you into a frame of mind to solve the issue.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  4. #4
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I usually just make spelling mistakes

  5. #5
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trogdor1024
    I usually just make spelling mistakes
    That is what error_reporting(E_ALL) is for
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  6. #6
    Non-Member
    Join Date
    Nov 2004
    Location
    Ithaca, NY
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I think you're right, it's a common experience. It certainly happens to me a lot.

    Actually, in this case, I decided to give up on this problem while awaiting responses and work on some different functionality. That made me take a closer look at some code I hadn't suspected of being involved with this problem, and there was the solution staring me in the face...

  7. #7
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Melbourne
    Posts
    362
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Was it referencing it as a static method?

  8. #8
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I had a dollar for every time I figure something out on my own immediately after posting the question on a forum I'd have eight or nine dollars by now.

    ...I might be onto something here

  9. #9
    Non-Member
    Join Date
    Nov 2004
    Location
    Ithaca, NY
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, the problem was an embarrassing, obvious syntax mistake. I just hadn't been looking for it in the right file.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •