Zend Framework Zend::updateRegister??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Zend Framework Zend::updateRegister??

Post by DaveTheAve »

As all Zend Framework programmers know the Zend::Register static function only allows you to register a object once. However, I need to update the object that was saved in the registry. How does one update a registered object?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Retrieve it and update the properties/methods as needed/allowed?
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post by DaveTheAve »

Ya, that isn't implemented in the Zend Framework as far as I know.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I think that was an intended design decision... I don't think your supposed to be able to resave anything into the registry...
Zend Framework Manual wrote:The $name argument must be a string, only objects may be stored in the registry, an object can never be removed from the registry, and an instance can only be stored once in the registry. Failure to obey these rules will result in Zend_Exception being thrown.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

DaveTheAve wrote:Ya, that isn't implemented in the Zend Framework as far as I know.
So you can register an object, but you can't retrieve the object?

I seriously doubt that.

Not being able to replace the registered object has no baring on being able to set properties in the object.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yes you can definately retrieve it and change properties once you retrieve it, but no resaving! :P
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why would you need to save it again? You're altering the reference returned from the framework, aren't you? ;)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Remember, objects are passed by reference by default in php5.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

which is why
I think that was an intended design decision
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post by DaveTheAve »

Alright in the bootstrap file I register the 'View', then in NavigationalPanel.php I add the, well, navigational panel to 'view', then in the Action Controller i need to use 'view' to render the display. This of-course is only one rather small example, but it does indeed stretch beyond this.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't see how that leads to needing to register the class again. Your navigation panel is added to the view, which is the instance returned from the framework's registry. If you pulled out the view correctly (as a reference) you'd be editing the one in the registry..

Do I need to keep repeating myself?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Here is an example for you to play around with...

Code: Select all

<?php
/**
 * Test object to show you how objects are handled by reference
 */
class TestObject
{

	public $foobar = null;

	public function __construct($value = null)
	{
		$this->foobar = $value;
	}
	
}
/**
 * Test registry
 */
class SingleRegistry
{

	protected $_value = null;

	public function set($value)
	{
		if(is_null($this->_value))
		{
			if(is_object($value))
			{
				$this->_value = $value;
				return;
			}
			throw new Exception('Value must be an object');
		}
		throw new Exception('You can only store one value in the registry, and you can not resave it. (no need to)');

	}
	
	public function get()
	{
		return $this->_value;
	}
	
}

$Registry = new SingleRegistry; // Instantiate Registry
$Object = new TestObject('foobar'); // Instantiate our test object (to be registered)

try
{
	$Registry->set($Object); // Set the object into the registry
	
?><pre><?php
print_r($Registry); // See how our test object looks in the registry (foobar should be set to 'foobar')
?></pre><?php
	
	$ObjectAgain = $Registry->get(); // Retrieve our object from registry
	$ObjectAgain->foobar = 'test'; // Change it's values
}
catch (Exception $e)
{
	echo $e;
}


?><pre><?php
print_r($Registry); // Check how the test object looks again HEY WOW! It's changed! PHP5 ROCKS!!!
?></pre>
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post by DaveTheAve »

feyd wrote:Do I need to keep repeating myself?
Sorry, I must have not caught that; I understand that I would need to use the &$variable method. How would one this with the Zend Registry methods?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

You don't need to... PHP5 does it for you (which is required for Zend Framework to work). Look here:
http://www.zend.com/php5/articles/engin ... hanges.php
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I am not sure you are looking at this from the right perspective. I am not intending to insult you, I am merely recalling an experience I had recently with a registry object.

The registry is used as a sort of store that allows the checking in and checking out of objects, vars, etc. Once an item is registered it is not changeable (hence keeping its global property, if you will). When dealing with registering an object, all methods and properties of the object stay with that object (in PHP5). So if you instantiate an object, set some object properties, register the object, then call that object from elsewhere within the app and set more object properties, all set properties will be available to you in within that object.

Is this at all related to what you want to know?
Post Reply