// Creating a registry object class Registry { private static $instance; private $data = array(); private function __construct() {} public static function getInstance() { if (!isset(self::$instance)) { self::$instance = new Registry(); } return self::$instance; } public function set($key, $value) { $this->data[$key] = $value; } public function get($key) { return $this->data[$key]; } } // Using the registry object $registry = Registry::getInstance(); $registry->set('foo', 'bar'); echo $registry->get('foo'); // Output: 'bar'
// Using a registry class from a package library require_once('vendor/autoload.php'); // Creating a registry object $registry = new \Symfony\Component\DependencyInjection\ContainerBuilder(); // Setting a parameter $registry->setParameter('my_param', 'hello world'); // Retrieving a parameter $val = $registry->getParameter('my_param'); // Output: 'hello world'In the first example, a custom registry object is created and used to store a key-value pair. The `getInstance()` method ensures that only one instance of the object is created throughout the application. In the second example, a third-party package library is used to create a registry. The `setParameter()` and `getParameter()` methods are used to set and retrieve a parameter, respectively. The library used here is Symfony's DependencyInjection component, which provides a way to register and manage services in an application.