Perhaps i'm over thinking the Registry?

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
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Perhaps i'm over thinking the Registry?

Post 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 :)
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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;
}
(#10850)
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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...
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post 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
Post Reply