Page 1 of 2
Zend Framework Zend::updateRegister??
Posted: Thu Dec 07, 2006 3:28 pm
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?
Posted: Thu Dec 07, 2006 3:53 pm
by feyd
Retrieve it and update the properties/methods as needed/allowed?
Posted: Thu Dec 07, 2006 4:39 pm
by DaveTheAve
Ya, that isn't implemented in the Zend Framework as far as I know.
Posted: Thu Dec 07, 2006 4:50 pm
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.
Posted: Thu Dec 07, 2006 4:54 pm
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.
Posted: Thu Dec 07, 2006 4:58 pm
by Luke
yes you can definately retrieve it and change properties once you retrieve it, but no resaving!

Posted: Thu Dec 07, 2006 5:50 pm
by feyd
Why would you need to save it again? You're altering the reference returned from the framework, aren't you?

Posted: Thu Dec 07, 2006 5:53 pm
by John Cartwright
Remember, objects are passed by reference by default in php5.
Posted: Thu Dec 07, 2006 5:53 pm
by Luke
which is why
I think that was an intended design decision
Posted: Thu Dec 07, 2006 6:47 pm
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.
Posted: Thu Dec 07, 2006 9:17 pm
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?
Posted: Fri Dec 08, 2006 10:37 am
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>
Posted: Fri Dec 08, 2006 10:48 am
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?
Posted: Fri Dec 08, 2006 10:59 am
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
Posted: Fri Dec 08, 2006 11:03 am
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?