I would like to point out that early on in the development of Zend_Registry I had advocated providing the option of using it via dependency injection, but was summarily overruled. ;-) -Matt
On Sat, Jul 11, 2009 at 1:21 PM, Ralph Schindler <[email protected]>wrote: > This is effectively a named registry. I advocate the usage of named > registries as a way to be able to group instances together into collections > at named locations. > > You could similarly, do this with your own class: > > class My_Application { > static function get($name); > static function set($name, $value); > } > > as opposed to > > function Application($name) { .. } > > > the only difference is one solution is using an OO paradigm whereas the > other is using a functional paradigm. The result is the same. > > I like advocating this b/c you are always 1 method call away from having > the instance you want, which makes programming easy to write and easy to > understand. > > OTHER solutions posed by my peers would be a Dependency Injection Container > (you can google this). This has the added benefit of resolving dependecies > as well as "automatic wiring" of instance. The downside is its a little > heavier, and a little more complex (ie: takes more time to understand). But > in larger code-bases, it has its pros. Also, it does make things easier to > test against as static state does not get in the way as much. > > -ralph > > > > Bruno B. B. Magalhaes wrote: > >> Hi everybody, >> >> first of all, this is my first post on this list, but I've been using this >> framework for a long time, since it's very early stages, and it has been >> very fun since... >> >> Yesterday I was working in a new client's infrastructure (we have to by >> contract) for a very large company in Brazil, and I had a simple idea when >> using a registry with variable scopes... For example the controller can only >> access the instances of database, router, request and response, the plugin >> can only use the configuration, database, request and response objects... >> How to deal with this kind of variable scope registry... Here is my >> suggestion: >> >> function Application($Name = NULL, $Instance = NULL) >> { >> static $Classes = array(); >> static $Instances = array(); >> if(empty($Name)) >> { >> throw new Application_Exception('Unable to utilize this instance >> (`'.$Name.'`): The supplied instance name must not be empty.'); >> } >> >> if(is_object($Instance)) >> { >> $Classes[get_class($Instance)] = $Name; >> >> $Instances[$Name] = $Instance; >> return $Instance; >> } >> elseif(isset($Instances[$Name])) >> { >> return $Instances[$Name]; >> } >> elseif(isset($Instances[$Classes[$Name]])) >> { >> return $Instances[$Classes[$Name]]; >> } >> else >> { >> throw new Application_Exception('Unable to utilize this instance >> (`'.$Name.'`): It was not found within this specific scope >> (`application`).'); >> } >> } >> >> For the controller scope: >> >> function Controller($Name = NULL, $Instance = NULL) >> { >> static $Classes = array(); >> static $Instances = array(); >> if(empty($Name)) >> { >> throw new Application_Controller_Exception('Unable to utilize this >> instance (`'.$Name.'`): The supplied instance name must not be empty.'); >> } >> >> if(is_object($Instance)) >> { >> $Classes[get_class($Instance)] = $Name; >> >> $Instances[$Name] = $Instance; >> return $Instance; >> } >> elseif(isset($Instances[$Name])) >> { >> return $Instances[$Name]; >> } >> elseif(isset($Instances[$Classes[$Name]])) >> { >> return $Instances[$Classes[$Name]]; >> } >> else >> { >> throw new Application_Controller_Exception('Unable to utilize this >> instance (`'.$Name.'`): It was not found within this specific scope >> (`controller`).'); >> } >> } >> >> And for the plugin scope >> >> function Plugin($Name = NULL, $Instance = NULL) >> { >> static $Classes = array(); >> static $Instances = array(); >> if(empty($Name)) >> { >> throw new Application_Plugin_Exception('Unable to utilize this >> instance (`'.$Name.'`): The supplied instance name must not be empty.'); >> } >> >> if(is_object($Instance)) >> { >> $Classes[get_class($Instance)] = $Name; >> >> $Instances[$Name] = $Instance; >> return $Instance; >> } >> elseif(isset($Instances[$Name])) >> { >> return $Instances[$Name]; >> } >> elseif(isset($Instances[$Classes[$Name]])) >> { >> return $Instances[$Classes[$Name]]; >> } >> else >> { >> throw new Application_Plugin_Exception('Unable to utilize this >> instance (`'.$Name.'`): It was not found within this specific scope >> (`plugin`).'); >> } >> } >> >> We would add instances to the registry like: >> Application('Configuration', new Zend_Config()); >> Controller('Configuration', Application('Configuration')); >> Plugin('Configuration', Application('Configuration')); >> >> So we are able to use it like this, inside of a controller: >> >> Application('Configuration')->GerParameter('application.address'); OR >> Controller('Configuration')->GerParameter('application.address'); >> >> Os if this is available inside the plugin, we can use: >> >> Application('Configuration')->GerParameter('application.address'); OR >> Plugin('Configuration')->GerParameter('application.address'); >> >> Maybe we could have a function called Zend(), to "abstract" the Zend >> Registry? It makes the code extremely readable and fluid... Guys, am I >> crazy? Is this a pattern of somehow? :D >> >> Best regards, >> Bruno B B Magalhaes >> >>
