Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Registry Pattern<br />The<br />with lazy loading goodness<br />Michael Peacock<br />
whois michaelpeacock.co.uk<br />Experienced senior / lead web developer<br />Web Systems Developer for SmithElectric Vehic...
Commonly used objects and settings...<br />Within Bespoke frameworks and large applications most of the code often needs a...
...such as<br />Database Access / Abstraction layers<br />Template engines<br />Object for the currently logged in user<br...
The solution: Registry<br />Create a registry to store these core objects and settings<br />Provide methods to store and r...
Registry: basics<br /><?php<br />	class Registry {<br />		private $objects = array();<br />		private $settings = array();<...
Registry: concept code<br />public function storeObject( $object, $key )<br />{<br />	if( array_key_exists( $key, $this->o...
Registry: usage<br />Make your code aware<br />Public function __construct( Registry $registry)<br />{<br />	$this->regist...
Registry: The good<br />Keeps core objects and settings in one place<br />Makes it easy to access and use these objects an...
Registry: The bad<br />All aspects of your application need to be registry aware...				...though its easy to simply pass t...
Registry: The ugly<br />Bloat!<br />If you rely on the registry too much, you end up with objects or settings which you on...
Registry: de-uglification<br />Make the registry lazy loading<br />Registry is aware of all of its objects<br />Objects ar...
De-uglification: <br />Make the registry aware of its objects<br />$defaultRegistryObjects = array(); <br />$db = array( '...
De-uglification<br />    If the object isn’t set, load it the lazy way<br />/** <br />* Get an object from the registry<br...
De-uglification: settings<br />Apply the same approach to settings / data<br />Query your core settings<br />If the settin...
Conclusion<br />Easily access your core objects, functionality and data from anywhere in your application<br />Apply some ...
Any questions?<br />www.michaelpeacock.co.uk<br />www.twitter.com/michaelpeacock<br />www.invoicecentral.co.uk<br />
Upcoming SlideShare
Loading in …5
×

PHP North East - Registry Design Pattern

940 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

PHP North East - Registry Design Pattern

  1. 1. Registry Pattern<br />The<br />with lazy loading goodness<br />Michael Peacock<br />
  2. 2. whois michaelpeacock.co.uk<br />Experienced senior / lead web developer<br />Web Systems Developer for SmithElectric Vehicles US Corp<br />Author of 6 web development books<br />Owner / CTO of CentralAppsLimitedJust launched first beta product: invoicecentral.co.uk<br />Zend Certified Engineer<br />
  3. 3. Commonly used objects and settings...<br />Within Bespoke frameworks and large applications most of the code often needs access to core objects, settings and variables.<br />
  4. 4. ...such as<br />Database Access / Abstraction layers<br />Template engines<br />Object for the currently logged in user<br />Objects which perform common tasks, such as email sending or data processing<br />Site settings: path to uploads folder, cache folder, site URL, etc<br />
  5. 5. The solution: Registry<br />Create a registry to store these core objects and settings<br />Provide methods to store and retrieve these objects and settings<br />Pass this object around your application (e.g. To your models and controllers) –or- make it a singleton (yuk)<br />
  6. 6. Registry: basics<br /><?php<br /> class Registry {<br /> private $objects = array();<br /> private $settings = array();<br /> public function __construct(){}<br />public function storeObject( $object, $key ){}<br /> public function getObject( $key ){}<br /> public function storeSetting( $setting, $key ){}<br /> public function getSetting( $key ){}<br /> }<br />?><br />
  7. 7. Registry: concept code<br />public function storeObject( $object, $key )<br />{<br /> if( array_key_exists( $key, $this->objects ) )<br /> {<br /> //throw an exception<br /> }<br />elseif( is_object( $object ) )<br />{<br /> $this->objects[ $key ] = $object;<br /> }<br />}<br />public function getObject( $key )<br />{<br /> if( array_key_exists( $key, $this->objects ) && is_object( $this->objects[ $key ] ) )<br /> {<br /> return $this->objects[ $key ];<br /> }<br /> else<br /> {<br /> //throw an exception<br /> }<br />}<br />EASY!<br />
  8. 8. Registry: usage<br />Make your code aware<br />Public function __construct( Registry $registry)<br />{<br /> $this->registry = $registry;<br />}<br />Store an object<br />$this->registry->storeObject( $template, ‘template’ );<br />Access your objects<br />$this->registry->getObject(‘template’)->output();<br />
  9. 9. Registry: The good<br />Keeps core objects and settings in one place<br />Makes it easy to access and use these objects and the data or functionality they hold within<br />Common interface for storing, retrieving and managing common objects and settings<br />
  10. 10. Registry: The bad<br />All aspects of your application need to be registry aware... ...though its easy to simply pass the registry via the constructor to other parts of your code<br />Works best with a front controller / single entry point to your application, otherwise you need to duplicate your registry setup throughout your code<br />
  11. 11. Registry: The ugly<br />Bloat!<br />If you rely on the registry too much, you end up with objects or settings which you only use some of the time<br />This takes up additional resources, time and memory to setup and process the objects and settings for each page load, only for them to be used 50% of the time<br />
  12. 12. Registry: de-uglification<br />Make the registry lazy loading<br />Registry is aware of all of its objects<br />Objects are only instantiated and stored when they are first required, and not before<br />Registry knows about core settings/data that is used frequently, and knows how to access other data if/when required<br />
  13. 13. De-uglification: <br />Make the registry aware of its objects<br />$defaultRegistryObjects = array(); <br />$db = array( 'abstract' => 'database', 'folder' => 'database', 'file' => 'mysql.database', 'class' => 'MySQLDatabase', 'key' => 'db' ); <br />$defaultRegistryObjects['db'] = $db; $template = array( 'abstract' => null, 'folder' => 'template', 'file' => 'template', 'class' => 'Template', 'key' => 'template' ); <br />$defaultRegistryObjects['template'] = $template; $urlp = array( 'abstract' => null, 'folder' => 'urlprocessor', 'file' => 'urlprocessor', 'class' => 'URLProcessor', 'key' => 'urlprocessor' );<br />
  14. 14. De-uglification<br /> If the object isn’t set, load it the lazy way<br />/** <br />* Get an object from the registry<br />* - facilitates lazy loading, if we haven't used the object yet and it is part of the setup, then require and instantiate it! <br />* @param String $key<br />* @return Object<br /> */<br />public function getObject( $key ) <br />{ <br />if( in_array( $key, array_keys( $this->objects ) ) ) <br />{<br />return $this->objects[$key]; <br />}<br />elseif( in_array( $key, array_keys( $this->objectSetup ) ) ) <br />{<br /> if( ! is_null( $this->objectSetup[ $key ]['abstract'] ) ) <br /> { <br />require_once( FRAMEWORK_PATH . 'registry/aspects/' . $this->objectSetup[ $key ]['folder'] . '/' . $this->objectSetup[ $key ]['abstract'] .'.abstract.php' ); <br /> }<br />require_once( FRAMEWORK_PATH . 'registry/aspects/' . $this->objectSetup[ $key ]['folder'] . '/' . $this->objectSetup[ $key ]['file'] . '.class.php' ); <br /> $o = new $this->objectSetup[ $key ]['class']( $this ); <br /> $this->storeObject( $o, $key ); <br /> return $o; <br />}<br />}<br />
  15. 15. De-uglification: settings<br />Apply the same approach to settings / data<br />Query your core settings<br />If the setting requested hasn’t been loaded, load all related settings (works best if you prefix your keys with a group name)<br />
  16. 16. Conclusion<br />Easily access your core objects, functionality and data from anywhere in your application<br />Apply some lazy loading to keep your application running lean<br />Enjoy!<br />
  17. 17. Any questions?<br />www.michaelpeacock.co.uk<br />www.twitter.com/michaelpeacock<br />www.invoicecentral.co.uk<br />

×