Base patterns
In the coming section, we will take a look at the base pattern: the registry pattern.
The registry pattern
The registry pattern is an interesting one. It allows us to store and retrieve objects for later use. The process of storing and retrieving is based on the keys we define. Depending on the data scope, the association of keys and objects is made global across a process, thread, or a session, allowing us to retrieve the objects from anywhere within the data scope.
The following example demonstrates a possible registry pattern implementation:
<?php
class Registry
{
private
$registry = [];
public
function get($key)
{
if (isset($this->registry[$key])) {
return $this->registry[$key];
}
return null;
}
public
function set($key, $value, $graceful = false)
{
if (isset($this->registry[$key])) {
if ($graceful) {
return;
}
throw new \RuntimeException...