Pages

Search This Blog

Monday 15 June 2015

Implementation Of Registry Design Pattern in PHP (Practical Example)

<?php

/*
 *
 * @Author: Zuhair Mirza
 * @Implementation Of Registry Design Pattern in PHP (Practical Example)
 * @Date : 15-June-2015
 * @Defination  :  This pattern is a bit unusual from the overall list, because it is not a Creational pattern. Well, register – it is hash, and you access to data through the static methods.
 *
 */

/**
* Registry class
*/

class Registry {

    protected static $data = array();

    public static function set($key, $value) {
        self::$data[$key] = $value;
    }

    public static function get($key) {
        return isset(self::$data[$key]) ? self::$data[$key] : null;
    }

    final public static function removeObject($key) {
        if (array_key_exists($key, self::$data)) {
            unset(self::$data[$key]);
        }
    }
}

Registry::set('name', 'Registry Name');

print_r(Registry::get('name'));

// Output: Registry Name
?>

No comments:

Post a Comment