PHP Classes

File: src/App/Stash/Stash.php

Recommend this page to a friend!
  Packages of Thierry Feuzeu   Jaxon   src/App/Stash/Stash.php   Download  
File: src/App/Stash/Stash.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Jaxon
Call PHP classes from JavaScript using AJAX
Author: By
Last change:
Date: 1 year ago
Size: 795 bytes
 

 

Contents

Class file image Download
<?php

namespace Jaxon\App\Stash;

use function is_callable;

class Stash
{
    /**
     * @var array
     */
    private array $values = [];

    /**
     * @param string $key
     * @param mixed $value
     *
     * @return void
     */
    public function set(string $key, mixed $value): void
    {
        $this->values[$key] = $value;
    }

    /**
     * @param string $key
     * @param mixed $default
     *
     * @return mixed
     */
    public function get(string $key, mixed $default = null): mixed
    {
        $value = $this->values[$key] ?? $default;
        if(is_callable($value))
        {
            $value = $value();
            // Save the value returned by the callback in the stash.
            $this->values[$key] = $value;
        }

        return $value;
    }
}