phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


OOP Pattern - Registry: Introduction Registry Pattern

Tag: oop, pattern, registry   Category: PHP Classes
post: 21 Feb 2008 read: 672


PHP Registry Pattern Step By Step Tutorial - Part 1: Actually, we involve many parameters. We put in and out parameters in many method. It will raise what we name "tramp data". To make everything clear, we can implement Registry pattern.

Following sample to store and get parameters.


<?php
class CarRegistry{
  var $_store = array();
    
  function set($key, $obj){
    $this->_store[$key] = $obj;
  }
  
  function get($key){
    if(array_key_exists($key, $this->_store)){
		return $this->_store[$key];
	}
  }
  
  function &getInstance(){
    static $instance = array();
	if (!$instance){
	  $instance[0] =& new CarRegistry;
	  return $instance[0];
	}
  }
}

// TEST IT
$reg =& CarRegistry::getInstance();

$key = 'honda';
$value = array('Jazz', 'Stream');
$reg->set($key,$value);

$key = 'nissan';
$value = array('Teranno', 'Livina');
$reg->set($key,$value);

echo "Honda: ".implode(", ",$reg->get('honda'));
echo "</br>";
echo "Nissan: ".implode(", ",$reg->get('nissan'));

// OUTPUT
// Honda: Jazz, Stream
// Nissan: Teranno, Livina
?>

set(for input/store parameters) and get(for retrieve parameters) are common name used registry pattern.

mmm... we can combine with Singleton pattern for above.




| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting