LENKRAD - The modern PHP core
Basics
Advanced
Deployment

Dynamic Store

Dynamic Store introduction

The Neoan\Store class

The dynamic store is a simple key-value storage meant to be written throughout the procedural execution while being writable and readable until rendering or responding. This is useful for default variable values when actual value can only be determined at a later point in time.

Static methods

Method
Description

Store::dynamic(string $variableName)

Returns an instance of "Dynamic"

Store::write(string $variable, mixed $value)

Writes to a store variable.

Store::readValue(string $variableName)

Retrieves the current value of a store variable

Store::getInstance()

Returns the instance of "Store"

The Dynamic class

The dynamic instance for a store variable allows us to break out of certain race conditions. This is best explained with how this documentation uses it: In our layout template, the HTML contains a title tag. However, we might not know the actual title of the page before executing all routable classes. Rather than having to worry about passing on and handling a variable, we can use a dynamic value and - if applicable - set its value whenever needed.


            ...
            $setup = new Setup();
            $setup->setPublicPath($root . '/public')
                  ->setSkeletonHTML('configuration/views/layout.html')
                  ->setSkeletonVariables(['title' => Store::dynamic('title')])
            ...
        

        

In this example we prepare for the variable "title" to exist at the time of rendering, although it has yet to be set (/written to).

Tip: In many cases a default value is useful in order to ensure availability. Use "Store::write" early or even before in your execution logic (maybe in your setup).

We can now decide to assign a value in any middleware or routable along the chain. For our example, we can add our custom page title in our route(s):


            #[Web('/docs/dynamic-store', '/docs/dynamicstore.html')]
            class DynamicStore implements Routable
            {
                public function __invoke(): array
                {
                    Store::write('title', 'DynamicStore');
                    return ['renderVariable' => 'exampleValue'];
                }
            }
        

Before you move on

Many references on this page assume default settings. Your project might differ in behavior, paths etc.