Page 1 of 1

Perhaps i'm over thinking the Registry?

Posted: Fri Nov 03, 2006 11:19 am
by MrPotatoes
Question. it's a bigun so pay attention ;). second part is a smllun


i have a loader class and it's basically a registry except that it actually instantiates the class within it and keeps the library and it's label within. no big deal. i just added a little bit of functionality to it.

i have all the basica functionality in here. get a library, register a library and all that good stuff. the restry is a very large global in memory (i wanted to keep it smaller but what can i do now ;). so that being said if i have a global and i register a class within it:

Code: Select all

$registry = new BlackBarnRegistry();
$registry->loadDebug('debug.View');
this is how i get the class and use it's functionality:

Code: Select all

$template = $load->get('View');
$template->set('Body', $BodyText);
$template->fetch('error.tpl');
well. i've set it and if i'm using an alias for that registry index in the registry array (oh man that's a mouthful) then that means that only that alias has that information. i should create a function within there that actually sets it with the registry correct?

Code: Select all

public function set($object, $label) 
{
	if (isset($this->library[$label]))
	{
		$this->library[$label] = $object;
		return true;
	}
	return false;
}
then use it

Code: Select all

$registry->set($template, 'View');
but this is where the problem is. i might be over thinking it but if i do that then i'm overwriting what was already in there and BAM! it's gone. i can't get to it again and i'm screwed.

well, i have to update what is within that class don't i? and if i do can i do this from the registry class or do i have to add that functionality to each and every class that i have?

also, second question. this class is a registry but it's also an autoloader class. to make it simpler for me. what should i name it? as it stands it's Loader. thoughts on that would be nice :)

Posted: Fri Nov 03, 2006 1:15 pm
by MrPotatoes
well i figured it out. this is the code that i used.

Code: Select all

$session  = $load->get('dook');

echo 'print alias:<BR>';
$session->printall();
echo '<BR>print original<BR>';
$load->get('dook')->printall();

echo 'modify alias (q=10, W=90)';
$session->q = 10;
$session->w = 90;

echo '<BR>print alias again:<BR>';
$session->printall();

echo 'print orignal again:<BR>';
$load->get('dook')->printall();
this is the output:

Code: Select all

print alias:
1
2
3
4

print original
1
2
3
4
modify alias (q=10, W=90)
print alias again:
10
90
3
4
print orignal again:
10
90
3
4
now. in C++ this wouldn't happen. i kinda want to know why i have it so easy right now. i'm sorry, i just get scared when anything works lmao

Posted: Fri Nov 03, 2006 2:48 pm
by Christopher
I don't claim to have followed much of that ... still confused, but shouldn't it be:

Code: Select all

public function set($object, $label)
{
        if ( !  isset($this->library[$label]))          // IF NOT SET THEN SET -- NO OVERWRITE ALLOWED
        {
                $this->library[$label] = $object;
                return true;
        }
        return false;
}

Posted: Fri Nov 03, 2006 2:57 pm
by nickvd
Not 100%, but from PHP5 onwards, objects are always passed by reference, so $session = $reg->get('class'); won't create a copy of the object, but assign a reference to the original to $session..

Please correct me if I'm wrong...

Posted: Fri Nov 03, 2006 3:10 pm
by MrPotatoes
nickvd wrote:Not 100%, but from PHP5 onwards, objects are always passed by reference, so $session = $reg->get('class'); won't create a copy of the object, but assign a reference to the original to $session..

Please correct me if I'm wrong...
i forgot everything was passed by referance. so if this is the case then this makes perfect sense and i this can be closed.

and yea, my set function is wrong but i've done removed it anyways as i don't actually need