SlideShare a Scribd company logo
Registry PatternThewith lazy loading goodnessMichael Peacock
whois michaelpeacock.co.ukExperienced senior / lead web developerWeb Systems Developer for SmithElectric Vehicles US CorpAuthor of 6 web development booksOwner / CTO of CentralAppsLimitedJust launched first beta product: invoicecentral.co.ukZend Certified Engineer
Commonly used objects and settings...Within Bespoke frameworks and large applications most of the code often needs access to core objects, settings and variables.
...such asDatabase Access / Abstraction layersTemplate enginesObject for the currently logged in userObjects which perform common tasks, such as email sending or data processingSite settings: path to uploads folder, cache folder, site URL, etc
The solution: RegistryCreate a registry to store these core objects and settingsProvide methods to store and retrieve these objects and settingsPass this object around your application (e.g. To your models and controllers) –or- make it a singleton (yuk)
Registry: basics<?php	class Registry {		private $objects = array();		private $settings = array();		public function __construct(){}public function storeObject( $object, $key ){}		public function getObject( $key ){}		public function storeSetting( $setting, $key ){}		public function getSetting( $key ){}	}?>
Registry: concept codepublic function storeObject( $object, $key ){	if( array_key_exists( $key, $this->objects ) )	{		//throw an exception	}elseif( is_object( $object ) ){	$this->objects[ $key ] = $object;	}}public function getObject( $key ){	if( array_key_exists( $key, $this->objects ) && is_object( $this->objects[ $key ] ) )	{		return $this->objects[ $key ];	}	else	{		//throw an exception	}}EASY!
Registry: usageMake your code awarePublic function __construct( Registry $registry){	$this->registry = $registry;}Store an object$this->registry->storeObject( $template, ‘template’ );Access your objects$this->registry->getObject(‘template’)->output();
Registry: The goodKeeps core objects and settings in one placeMakes it easy to access and use these objects and the data or functionality they hold withinCommon interface for storing, retrieving and managing common objects and settings
Registry: The badAll 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 codeWorks best with a front controller / single entry point to your application, otherwise you need to duplicate your registry setup throughout your code
Registry: The uglyBloat!If you rely on the registry too much, you end up with objects or settings which you only use some of the timeThis 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
Registry: de-uglificationMake the registry lazy loadingRegistry is aware of all of its objectsObjects are only instantiated and stored when they are first required, and not beforeRegistry knows about core settings/data that is used frequently, and knows how to access other data if/when required
De-uglification: Make the registry aware of its objects$defaultRegistryObjects = array(); $db = array( 'abstract' => 'database', 'folder' => 'database', 'file' => 'mysql.database', 'class' => 'MySQLDatabase', 'key' => 'db' ); $defaultRegistryObjects['db'] = $db; $template = array( 'abstract' => null, 'folder' => 'template', 'file' => 'template', 'class' => 'Template', 'key' => 'template' ); $defaultRegistryObjects['template'] = $template; $urlp = array( 'abstract' => null, 'folder' => 'urlprocessor', 'file' => 'urlprocessor', 'class' => 'URLProcessor', 'key' => 'urlprocessor' );
De-uglification    If the object isn’t set, load it the lazy way/** * Get an object from the registry* - facilitates lazy loading, if we haven't used the object yet and it is part of the setup, then require and instantiate it! * @param String $key* @return Object */public function getObject( $key ) { if( in_array( $key, array_keys( $this->objects ) ) ) {return $this->objects[$key]; }elseif( in_array( $key, array_keys( $this->objectSetup ) ) ) {	if( ! is_null( $this->objectSetup[ $key ]['abstract'] ) ) 	{ require_once( FRAMEWORK_PATH . 'registry/aspects/' . $this->objectSetup[ $key ]['folder'] . '/' . $this->objectSetup[ $key ]['abstract'] .'.abstract.php' ); 	}require_once( FRAMEWORK_PATH . 'registry/aspects/' . $this->objectSetup[ $key ]['folder'] . '/' . $this->objectSetup[ $key ]['file'] . '.class.php' ); 	$o = new $this->objectSetup[ $key ]['class']( $this ); 	$this->storeObject( $o, $key ); 	return $o; }}
De-uglification: settingsApply the same approach to settings / dataQuery your core settingsIf the setting requested hasn’t been loaded, load all related settings (works best if you prefix your keys with a group name)
ConclusionEasily access your core objects, functionality and data from anywhere in your applicationApply some lazy loading to keep your application running leanEnjoy!
Any questions?www.michaelpeacock.co.ukwww.twitter.com/michaelpeacockwww.invoicecentral.co.uk

More Related Content

What's hot (19)

PDF
3 introduction-php-mvc-cakephp-m3-getting-started-slides
MasterCode.vn
 
PPTX
L16 Object Relational Mapping and NoSQL
Ólafur Andri Ragnarsson
 
PDF
it's just search
Erik Hatcher
 
PPTX
Integration patterns in AEM 6
Yuval Ararat
 
PPTX
Visualizing drupalcode06 25-2015
Joe Tippetts
 
PDF
Codeigniter : Two Step View - Concept Implementation
Abdul Malik Ikhsan
 
PPTX
Migrate yourself. code -> module -> mind
Valentine Matsveiko
 
KEY
CakePHP REST Plugin
Kevin van Zonneveld
 
PPTX
Custom Database Queries in WordPress
topher1kenobe
 
PDF
Basics of Solr and Solr Integration with AEM6
DEEPAK KHETAWAT
 
PDF
Lucene for Solr Developers
Erik Hatcher
 
PDF
Extending the WordPress REST API - Josh Pollock
Caldera Labs
 
PDF
Apache Solr Workshop
Saumitra Srivastav
 
PDF
Compass Framework
Lukas Vlcek
 
PPT
Staging Drupal: Change Management Strategies for Drupal
Erich Beyrent
 
PPT
How to? Drupal developer toolkit. Dennis Povshedny.
DrupalCampDN
 
ODP
Dev8d Apache Solr Tutorial
Sourcesense
 
PPT
Introduction to Apache Solr.
ashish0x90
 
PPTX
Tutorial on developing a Solr search component plugin
searchbox-com
 
3 introduction-php-mvc-cakephp-m3-getting-started-slides
MasterCode.vn
 
L16 Object Relational Mapping and NoSQL
Ólafur Andri Ragnarsson
 
it's just search
Erik Hatcher
 
Integration patterns in AEM 6
Yuval Ararat
 
Visualizing drupalcode06 25-2015
Joe Tippetts
 
Codeigniter : Two Step View - Concept Implementation
Abdul Malik Ikhsan
 
Migrate yourself. code -> module -> mind
Valentine Matsveiko
 
CakePHP REST Plugin
Kevin van Zonneveld
 
Custom Database Queries in WordPress
topher1kenobe
 
Basics of Solr and Solr Integration with AEM6
DEEPAK KHETAWAT
 
Lucene for Solr Developers
Erik Hatcher
 
Extending the WordPress REST API - Josh Pollock
Caldera Labs
 
Apache Solr Workshop
Saumitra Srivastav
 
Compass Framework
Lukas Vlcek
 
Staging Drupal: Change Management Strategies for Drupal
Erich Beyrent
 
How to? Drupal developer toolkit. Dennis Povshedny.
DrupalCampDN
 
Dev8d Apache Solr Tutorial
Sourcesense
 
Introduction to Apache Solr.
ashish0x90
 
Tutorial on developing a Solr search component plugin
searchbox-com
 

Similar to PHP North East - Registry Design Pattern (20)

ODP
Object Oriented Design Patterns for PHP
RobertGonzalez
 
PPT
Go OO! - Real-life Design Patterns in PHP 5
Stephan Schmidt
 
PPT
Zend framework 03 - singleton factory data mapper caching logging
Tricode (part of Dept)
 
PDF
Design patterns
Jason Austin
 
ODP
Best practices tekx
Lorna Mitchell
 
PPT
Zend framework 06 - zend config, pdf, i18n, l10n, sessions
Tricode (part of Dept)
 
PDF
Intro To Mvc Development In Php
funkatron
 
PPT
Hanoi php day 2008 - 01.pham cong dinh - how.to.build.your.own.framework
Nguyen Duc Phu
 
PPT
How to learn to build your own PHP framework
Dinh Pham
 
PDF
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 
PPTX
Good practices for PrestaShop code security and optimization
PrestaShop
 
PPT
Patterns in PHP
Diego Lewin
 
PDF
PHP traits, treat or threat?
Nick Belhomme
 
PPT
OOP
thinkphp
 
ODP
Silverstripe 2.4-highlights-gpmd
GPMD Ltd
 
PPT
PHP - Procedural To Object-Oriented
Vance Lucas
 
KEY
CICONF 2012 - Don't Make Me Read Your Mind
ciconf
 
PDF
Object Oriented Programming in PHP
Lorna Mitchell
 
PDF
OOPs Concept
Mohammad Yousuf
 
ODP
Clean code for WordPress
mtoppa
 
Object Oriented Design Patterns for PHP
RobertGonzalez
 
Go OO! - Real-life Design Patterns in PHP 5
Stephan Schmidt
 
Zend framework 03 - singleton factory data mapper caching logging
Tricode (part of Dept)
 
Design patterns
Jason Austin
 
Best practices tekx
Lorna Mitchell
 
Zend framework 06 - zend config, pdf, i18n, l10n, sessions
Tricode (part of Dept)
 
Intro To Mvc Development In Php
funkatron
 
Hanoi php day 2008 - 01.pham cong dinh - how.to.build.your.own.framework
Nguyen Duc Phu
 
How to learn to build your own PHP framework
Dinh Pham
 
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 
Good practices for PrestaShop code security and optimization
PrestaShop
 
Patterns in PHP
Diego Lewin
 
PHP traits, treat or threat?
Nick Belhomme
 
Silverstripe 2.4-highlights-gpmd
GPMD Ltd
 
PHP - Procedural To Object-Oriented
Vance Lucas
 
CICONF 2012 - Don't Make Me Read Your Mind
ciconf
 
Object Oriented Programming in PHP
Lorna Mitchell
 
OOPs Concept
Mohammad Yousuf
 
Clean code for WordPress
mtoppa
 
Ad

More from Michael Peacock (20)

PPTX
Immutable Infrastructure with Packer Ansible and Terraform
Michael Peacock
 
PPTX
Test driven APIs with Laravel
Michael Peacock
 
PPTX
Symfony Workflow Component - Introductory Lightning Talk
Michael Peacock
 
PPTX
Alexa, lets make a skill
Michael Peacock
 
PPTX
API Development with Laravel
Michael Peacock
 
PPTX
An introduction to Laravel Passport
Michael Peacock
 
PDF
Phinx talk
Michael Peacock
 
PDF
Refactoring to symfony components
Michael Peacock
 
PPT
Dance for the puppet master: G6 Tech Talk
Michael Peacock
 
PPT
Powerful and flexible templates with Twig
Michael Peacock
 
PPT
Introduction to OOP with PHP
Michael Peacock
 
KEY
Vagrant
Michael Peacock
 
KEY
Phpne august-2012-symfony-components-friends
Michael Peacock
 
KEY
Evolution of a big data project
Michael Peacock
 
PPTX
Real time voice call integration - Confoo 2012
Michael Peacock
 
PPTX
Dealing with Continuous Data Processing, ConFoo 2012
Michael Peacock
 
PPTX
Data at Scale - Michael Peacock, Cloud Connect 2012
Michael Peacock
 
PPTX
Supermondays twilio
Michael Peacock
 
PPTX
PHP & Twilio
Michael Peacock
 
PPTX
PHP Continuous Data Processing
Michael Peacock
 
Immutable Infrastructure with Packer Ansible and Terraform
Michael Peacock
 
Test driven APIs with Laravel
Michael Peacock
 
Symfony Workflow Component - Introductory Lightning Talk
Michael Peacock
 
Alexa, lets make a skill
Michael Peacock
 
API Development with Laravel
Michael Peacock
 
An introduction to Laravel Passport
Michael Peacock
 
Phinx talk
Michael Peacock
 
Refactoring to symfony components
Michael Peacock
 
Dance for the puppet master: G6 Tech Talk
Michael Peacock
 
Powerful and flexible templates with Twig
Michael Peacock
 
Introduction to OOP with PHP
Michael Peacock
 
Phpne august-2012-symfony-components-friends
Michael Peacock
 
Evolution of a big data project
Michael Peacock
 
Real time voice call integration - Confoo 2012
Michael Peacock
 
Dealing with Continuous Data Processing, ConFoo 2012
Michael Peacock
 
Data at Scale - Michael Peacock, Cloud Connect 2012
Michael Peacock
 
Supermondays twilio
Michael Peacock
 
PHP & Twilio
Michael Peacock
 
PHP Continuous Data Processing
Michael Peacock
 
Ad

Recently uploaded (20)

PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
July Patch Tuesday
Ivanti
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 

PHP North East - Registry Design Pattern

  • 1. Registry PatternThewith lazy loading goodnessMichael Peacock
  • 2. whois michaelpeacock.co.ukExperienced senior / lead web developerWeb Systems Developer for SmithElectric Vehicles US CorpAuthor of 6 web development booksOwner / CTO of CentralAppsLimitedJust launched first beta product: invoicecentral.co.ukZend Certified Engineer
  • 3. Commonly used objects and settings...Within Bespoke frameworks and large applications most of the code often needs access to core objects, settings and variables.
  • 4. ...such asDatabase Access / Abstraction layersTemplate enginesObject for the currently logged in userObjects which perform common tasks, such as email sending or data processingSite settings: path to uploads folder, cache folder, site URL, etc
  • 5. The solution: RegistryCreate a registry to store these core objects and settingsProvide methods to store and retrieve these objects and settingsPass this object around your application (e.g. To your models and controllers) –or- make it a singleton (yuk)
  • 6. Registry: basics<?php class Registry { private $objects = array(); private $settings = array(); public function __construct(){}public function storeObject( $object, $key ){} public function getObject( $key ){} public function storeSetting( $setting, $key ){} public function getSetting( $key ){} }?>
  • 7. Registry: concept codepublic function storeObject( $object, $key ){ if( array_key_exists( $key, $this->objects ) ) { //throw an exception }elseif( is_object( $object ) ){ $this->objects[ $key ] = $object; }}public function getObject( $key ){ if( array_key_exists( $key, $this->objects ) && is_object( $this->objects[ $key ] ) ) { return $this->objects[ $key ]; } else { //throw an exception }}EASY!
  • 8. Registry: usageMake your code awarePublic function __construct( Registry $registry){ $this->registry = $registry;}Store an object$this->registry->storeObject( $template, ‘template’ );Access your objects$this->registry->getObject(‘template’)->output();
  • 9. Registry: The goodKeeps core objects and settings in one placeMakes it easy to access and use these objects and the data or functionality they hold withinCommon interface for storing, retrieving and managing common objects and settings
  • 10. Registry: The badAll 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 codeWorks best with a front controller / single entry point to your application, otherwise you need to duplicate your registry setup throughout your code
  • 11. Registry: The uglyBloat!If you rely on the registry too much, you end up with objects or settings which you only use some of the timeThis 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
  • 12. Registry: de-uglificationMake the registry lazy loadingRegistry is aware of all of its objectsObjects are only instantiated and stored when they are first required, and not beforeRegistry knows about core settings/data that is used frequently, and knows how to access other data if/when required
  • 13. De-uglification: Make the registry aware of its objects$defaultRegistryObjects = array(); $db = array( 'abstract' => 'database', 'folder' => 'database', 'file' => 'mysql.database', 'class' => 'MySQLDatabase', 'key' => 'db' ); $defaultRegistryObjects['db'] = $db; $template = array( 'abstract' => null, 'folder' => 'template', 'file' => 'template', 'class' => 'Template', 'key' => 'template' ); $defaultRegistryObjects['template'] = $template; $urlp = array( 'abstract' => null, 'folder' => 'urlprocessor', 'file' => 'urlprocessor', 'class' => 'URLProcessor', 'key' => 'urlprocessor' );
  • 14. De-uglification If the object isn’t set, load it the lazy way/** * Get an object from the registry* - facilitates lazy loading, if we haven't used the object yet and it is part of the setup, then require and instantiate it! * @param String $key* @return Object */public function getObject( $key ) { if( in_array( $key, array_keys( $this->objects ) ) ) {return $this->objects[$key]; }elseif( in_array( $key, array_keys( $this->objectSetup ) ) ) { if( ! is_null( $this->objectSetup[ $key ]['abstract'] ) ) { require_once( FRAMEWORK_PATH . 'registry/aspects/' . $this->objectSetup[ $key ]['folder'] . '/' . $this->objectSetup[ $key ]['abstract'] .'.abstract.php' ); }require_once( FRAMEWORK_PATH . 'registry/aspects/' . $this->objectSetup[ $key ]['folder'] . '/' . $this->objectSetup[ $key ]['file'] . '.class.php' ); $o = new $this->objectSetup[ $key ]['class']( $this ); $this->storeObject( $o, $key ); return $o; }}
  • 15. De-uglification: settingsApply the same approach to settings / dataQuery your core settingsIf the setting requested hasn’t been loaded, load all related settings (works best if you prefix your keys with a group name)
  • 16. ConclusionEasily access your core objects, functionality and data from anywhere in your applicationApply some lazy loading to keep your application running leanEnjoy!