Jump to content

Recommended Posts

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
                        )

                )

        )
}

)

Link to comment
https://forums.phpfreaks.com/topic/245719-php-registry-class-error/
Share on other sites

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.

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];
	}
}
}
?>

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;
}

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.

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;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.