Destramic Posted August 26, 2011 Share Posted August 26, 2011 hey guys im having an error whilst trying to return an object registered in my registry class. Notice: Trying to get property of non-object in C:\www\private\application\models\league_model.class.php on line 10 the object in trying to get is a config document reader which should return things such as database details and setting of the website. if someone could help me please that would be excellent...i hope you understand Index - where the resigtry is made $config_root = 'config' . DS . 'config.ini'; $document = new Document; $config = $document->read($config_root); $registry = new Registry(); $registry->config = $config; Model - where the config is called but returns an error $config = Registry::get_instance()->config; echo $config->db_username; Registry class <?php class Registry { static protected $_instance = null; protected $_objects = array(); static public function get_instance() { if (self::$_instance == null) { self::$_instance = new self(); } return self::$_instance; } public function __set($key, $object) { $this->_objects[$key] = $object; } public function __get($key) { if (isset($this->_objects[$key])) { return $this->_objects[$key]; } return NULL; } } ?> return of the instant $this->_objects within the Registry class Array ( [config] => Document Object ( [_values:protected] => Array ( [developement_enviroment] => true [db_type] => mysql [db_host] => localhost [db_username] => root [db_password] => password [db_database_name] => test [default_controller] => news [default_action] => articles [autoloader_ignore_directories] => Array ( [0] => .buildpath [1] => .project [2] => .settings [3] => . [4] => .. [5] => tmp [6] => views [7] => public [8] => scripts [9] => .htaccess ) ) ) } ) Quote Link to comment https://forums.phpfreaks.com/topic/245719-php-registry-class-error/ Share on other sites More sharing options...
teynon Posted August 26, 2011 Share Posted August 26, 2011 It appears your code is referencing this line: if (self::$_instance == null) Perhaps you can try: if (!isset(self::$_instance)) I'm not certain, as I haven't used it this way. However, if that doesn't fix it, you could do: if (@self::$_instance == null) Quote Link to comment https://forums.phpfreaks.com/topic/245719-php-registry-class-error/#findComment-1262093 Share on other sites More sharing options...
Destramic Posted August 26, 2011 Author Share Posted August 26, 2011 yeah i think the problem is getting the instance...ive tried what you said and it doest work at all sorry Quote Link to comment https://forums.phpfreaks.com/topic/245719-php-registry-class-error/#findComment-1262094 Share on other sites More sharing options...
teynon Posted August 26, 2011 Share Posted August 26, 2011 Besides the notice / warning message, is the program not doing what it is suppose to? If it is doing what it's suppose to, until someone else here comes along and lets you know how to fix it, (which will be easier if your post all of your code), you can put an @ on the line that is throwing the error. I don't know for sure which line it is that has that error (whatever line 10 is), but if you put an @ at the beginning of it it will basically mute the error. I would still make sure you find out why it is giving you an error. Quote Link to comment https://forums.phpfreaks.com/topic/245719-php-registry-class-error/#findComment-1262095 Share on other sites More sharing options...
Destramic Posted August 26, 2011 Author Share Posted August 26, 2011 its not returning any value at all...that is the problem. line 10 where the error is $config = Registry::get_instance()->config; Quote Link to comment https://forums.phpfreaks.com/topic/245719-php-registry-class-error/#findComment-1262233 Share on other sites More sharing options...
KevinM1 Posted August 26, 2011 Share Posted August 26, 2011 What's your Document class look like? Quote Link to comment https://forums.phpfreaks.com/topic/245719-php-registry-class-error/#findComment-1262242 Share on other sites More sharing options...
Destramic Posted August 26, 2011 Author Share Posted August 26, 2011 here you go...i hope someone can help...i know it is a small problem somewhere but i cant see where...to me the docuemt class works fine....but thanks @nightsylr document class <?php class Document { protected $_values = array(); function read($file_name) { $file_root = PRIVATE_DIRECTORY . $file_name; try { $file = fopen($file_root, 'r'); if (!file) { throw new Document_Exception(sprintf("Document '%s' was unable to be read.<br />\n", $file)); } else if (!file_exists($file_root)) { throw new Document_Exception(sprintf("Document '%s' doesn't exsist.<br />\n", $file)); } while ($i = fgets($file)) { if (!preg_match('/^\s*$/', $i)) { if (preg_match('/([A-Za-z0-9_]+) += +([A-Za-z0-9_.]+)/', $i, $found)) { $key = $found[1]; $value = $found[2]; $this->_values[$key] = $value; } else if (preg_match('/([A-Za-z0-9_]+)\[\] += +([A-Za-z0-9_.]+)/', $i, $found)) { $key = $found[1]; $value = $found[2]; if (!array_key_exists($key, $this->_values)) { $this->_values[$key] = array($value); } else { array_push($this->_values[$key], $value); } } } } fclose($file); return $this; } catch (Document_Exception $e) { echo $e->getMessage(); } } public function __get($key) { if (array_key_exists($key, $this->_values)) { return $this->_values[$key]; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245719-php-registry-class-error/#findComment-1262315 Share on other sites More sharing options...
KevinM1 Posted August 26, 2011 Share Posted August 26, 2011 Where is your model code in relation to your index code? I ask because it seems as though the config info is being lost somewhere along the way to you wanting to use it in your model. Quote Link to comment https://forums.phpfreaks.com/topic/245719-php-registry-class-error/#findComment-1262327 Share on other sites More sharing options...
Destramic Posted August 26, 2011 Author Share Posted August 26, 2011 i think there is something wrong with the registry class index - where tyhe registy is made $config_root = 'config' . DS . 'config.ini'; $document = new Document; $config = $document->read($config_root); $registry = new Registry(); $registry->config = $config; model - where i want the data returned public function __construct() { $config = Registry::get_instance()->config; echo $config->db_username; } Quote Link to comment https://forums.phpfreaks.com/topic/245719-php-registry-class-error/#findComment-1262333 Share on other sites More sharing options...
Destramic Posted August 26, 2011 Author Share Posted August 26, 2011 also i tried echo $registry->config->db_username; inside my index and it works fine....something wrong with the static instance?...i cant work it out Quote Link to comment https://forums.phpfreaks.com/topic/245719-php-registry-class-error/#findComment-1262334 Share on other sites More sharing options...
DavidAM Posted August 26, 2011 Share Posted August 26, 2011 In your Index you are using new to instantiate a Registry object. Then you assign the $config to a property of this new object. Later (in Model) you are calling the get_instance static method. This method is checking the static property $_instance. THIS PROPERTY IS NULL, so it creates another instance of the object which DOES NOT CONTAIN a value for the config property that you assigned TO THE OTHER INSTANCE. I suspect you should be using get_instance in the Index file instead of new. When I create a class that is intended to be a singleton (which is what it looks like you are trying to do), I usually add private function __construct() { } to the class. Using private should prevent the use of the new operator outside of the class itself. In other words, your get_instance will still be able to instantiate the object, but other code will NOT. Quote Link to comment https://forums.phpfreaks.com/topic/245719-php-registry-class-error/#findComment-1262354 Share on other sites More sharing options...
Destramic Posted August 26, 2011 Author Share Posted August 26, 2011 ok thank you so instead i would need to do $registry = new Registry()->get_instance(); this is the first time ive really used instances and singletons in a class Quote Link to comment https://forums.phpfreaks.com/topic/245719-php-registry-class-error/#findComment-1262382 Share on other sites More sharing options...
DavidAM Posted August 27, 2011 Share Posted August 27, 2011 No, you just use the get_instance static method, it returns an object instance: $config_root = 'config' . DS . 'config.ini'; $document = new Document; $config = $document->read($config_root); $registry = Registry::get_instance(); $registry->config = $config; Quote Link to comment https://forums.phpfreaks.com/topic/245719-php-registry-class-error/#findComment-1262542 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.