Registry problem

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Registry problem

Post by Luke »

I can't get my registry to work properly...

Code: Select all

class Registry{
        var $instances;
        function register($name, &$object) {
            $this->instances[$name] = $object;
        }
        function get($name) {
            return $this->instances[$name];
        }
}

class Error{
	var $errors = array();
	function push($error){
		$this->errors[] = $error;
	}
}

class MysqlConnect{
	function MysqlConnect($host, $user, $pass, &$registry){
		$this->host  = !empty($host) ? $host : false;
		$this->user  = !empty($user) ? $user : false;
		$this->pass  = !empty($pass) ? $pass : false;
		$this->error = $registry->get('error');
		
		if(@mysql_pconnect($host, $user, $pass)){
			echo "Connected!";
		}
		else{
			$this->error->push(mysql_error());
		}
		$this->error->push("hi");
	}
}

$registry = new Registry;

$error = new Error;
$registry->register('error', $error);

if(!empty($error->errors) && $GLOBALS['debug'] > 0) print_r($error->errors);
If I call $error->push, it is a different instance than if I call $registry->instances['error']->push... shouldn't they be the same since I am calling the objects by reference in my registry?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your assignment into the registry makes a copy.

Code: Select all

$this->instances[$name] = $object;
to

Code: Select all

$this->instances[$name] =& $object;
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Assuming it is an example/snippet, but you don't instantiate a Mysqlconnect object anywhere.. thus the error is never produced?

Anyway!

You need to use static properties, like so (not sure if 'static' keyword works in php4, but 'self' does):

Code: Select all

class Registry
{

    static var $instances = array();

    function Registry (&$object, $name)
    {
        self::$instances[$name] = $object;
    }

    function get ($name)
    {
        return self::$instances[$name];
    }
}
EDIT: or not, as the case may be..
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

hmm... still not working. I'll look through the rest of the code and see if I can find anything.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

The problem has something to do with this class:

Code: Select all

class MysqlConnect{
	function MysqlConnect($host, $user, $pass, &$registry){
		$this->host  = !empty($host) ? $host : false;
		$this->user  = !empty($user) ? $user : false;
		$this->pass  = !empty($pass) ? $pass : false;
		$this->error = $registry->get('error'); // This is not grabbing the correct instance of $registry or something
		
		if(@mysql_pconnect($host, $user, $pass)){
			echo "Connected!";
		}
		else{
			$this->error->push(mysql_error());
		}
		$this->error->push("hi");
	}
}
The mysql object gets instantiated elsewhere in the code... there is no problem there. I intentionally sent it the wrong credentials to see if the error class works... and it doesn't. :cry:
Last edited by Luke on Mon Jul 17, 2006 11:03 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

method 'get' should return references as well:

Code: Select all

function &get($name) {
    return $this->instances[$name];
}
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Weirdan wrote:method 'get' should return references as well:

Code: Select all

function &get($name) {
    return $this->instances[$name];
}
:D :D :D You're the man! thanks a lot!
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

And also you should catch get by reference:

Code: Select all

$this->error =& $registry->get('error');
Post Reply