@mac
The Registry design pattern is a way to store global variables in a centralized location and retrieve them easily throughout your application. Here's an example of how you can implement it in PHP:
1 2 3 4 5 6 7 8 9 10 11 |
class Registry { private static $registry = []; public static function set($key, $value) { self::$registry[$key] = $value; } public static function get($key) { return self::$registry[$key]; } } |
1 2 3 |
Registry::set('database_host', 'localhost'); Registry::set('database_user', 'root'); Registry::set('database_password', 'password123'); |
1 2 3 |
$database_host = Registry::get('database_host'); $database_user = Registry::get('database_user'); $database_password = Registry::get('database_password'); |
By using the Registry pattern, you can easily access and modify global variables throughout your application without having to pass them around as function arguments or class properties. However, keep in mind that overusing global variables can make your code harder to maintain and test, so use this pattern judiciously.