<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>spechal.com &#187; Patterns</title>
	<atom:link href="http://spechal.com/category/patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://spechal.com</link>
	<description>[spesh-uhl]</description>
	<lastBuildDate>Thu, 21 Jan 2010 11:50:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Registry Pattern Class for PHP5</title>
		<link>http://spechal.com/2009/11/16/php-registry-pattern-class-for-php5/</link>
		<comments>http://spechal.com/2009/11/16/php-registry-pattern-class-for-php5/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 14:08:18 +0000</pubDate>
		<dc:creator>Spechal</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://spechal.com/?p=34</guid>
		<description><![CDATA[The following class is used as a registry to hold elements.  Usage is as follows:
$registry = Registry::getInstance();
$registry->set('key', 'value');
echo $registry->get('key'); //prints value

&#60;?php
  /**
   * PHP5 Registry Class
   *
   * @package Registry
   * @author Travis Crowder &#60;travis.crowder@spechal.com&#62;
   * @see MIT License
   * @copyright [...]]]></description>
			<content:encoded><![CDATA[<p>The following class is used as a registry to hold elements.  Usage is as follows:</p>
<pre class="php" name="code">$registry = Registry::getInstance();
$registry->set('key', 'value');
echo $registry->get('key'); //prints value</pre>
<pre class="php" name="code">
&lt;?php
  /**
   * PHP5 Registry Class
   *
   * @package Registry
   * @author Travis Crowder &lt;travis.crowder@spechal.com&gt;
   * @see MIT License
   * @copyright Travis Crowder
   */

  /**
   *  Registry
   *
   *  The Registry class is used to store values in a global fashion
   *  @final
   *  @see ArrayObject
   */
  final class Registry extends ArrayObject {

    /**
     * Instance object
     *
     * @staticvar object Registry
     */
    static private $_instance = NULL;

    /**
     *  @access private
     *  @var mixed Array to hold the values.
     */
    private $_registry;

    /**
     * Using Singleton pattern with public constructor due to parent class
     *
     * @access public
     * @return void
     */
    public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS){
      parent::__construct($array, $flags);
    }

    /**
     * Singleton accessor to the object
     *
     * @access public
     * @return Registry $instance Instance of the object
     */
    static public function getInstance(){
      if(self::$_instance === NULL){
        self::$_instance = new Registry;
      }
      return self::$_instance;
    }

    /**
     *  Get method
     *
     *  @access public
     *  @return mixed Element in the registry
     */
    static public function get($key){
      $instance = self::getInstance();
      if(!$instance-&gt;checkOffset($key))
        return false; #throw new Exception($key . ' is not set in the registry');
      return $instance-&gt;returnOffset($key);
    }

    /**
     *  Set method
     *
     *  Accessor method used to set a value to a registry key.
     *  @access public
     *  @static
     *  @return void
     */
    static public function set($key, $value){
      $instance = self::getInstance();
      $instance-&gt;setOffset($key, $value);
    }

    /**
     *  Method to set ArrayObject offset
     *
     *  This method sets the ArrayObjects value
     *  @access protected
     *  @return void
     */
    protected function setOffset($key, $value){
      $this-&gt;_registry[$key] = $value;
    }

    /**
     *  Check ArrayObject offset
     *
     *  Method to check if an ArrayObject Registry index has a value.
     *  @param string $key Index of Registry to access
     *  @access protected
     *  @return bool
     */
    protected function checkOffset($key){
      if(isset($this-&gt;_registry[$key]))
        return true;

      return false;
    }

    /**
     *  Return method
     *
     *  Method to return the value of a Registry index
     *  @access protected
     *  @return mixed Value of Registry index.
     */
    protected function returnOffset($key){
      return $this-&gt;_registry[$key];
    }

    /**
     *  Dump method
     *
     *  Method to dump the contents of the Registry to the screen via print_r
     *  @access public
     *  @return void
     */
    public function dump(){
      print_r($this-&gt;_registry);
    }

  }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://spechal.com/2009/11/16/php-registry-pattern-class-for-php5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
