Laze 1.1.1 · Library for PHP
Values computed once, when first needed
Laze stores named values as closures. Nothing runs until a value is read for the first time; then the result is kept and cannot change. Until that first read a value can still be replaced, so a test can swap it for a mock, and constraints check every value as it is computed.
Free software (GPL 3) · PHP 8.0 or newer · one class, no dependencies
- define()nothing runs yet
- First read()the closure runs
- Next read()the stored value
- define() againignored: it is fixed
Examples
What sets it apart
Each example is a complete script, run with Laze 1.1.1. The output is what it printed.
Lazy
Defining a value costs nothing. The first read runs its closure; the second reuses the result.
<?php require __DIR__ . '/../vendor/autoload.php'; use divengine\laze; // Defining costs nothing: the closure does not run yet laze::define('DB_DSN', function () { echo "(reading the configuration)\n"; return 'pgsql:host=db;dbname=shop'; }); echo "Defined. Evaluated yet? ", laze::evaluated('DB_DSN') ? 'yes' : 'no', "\n"; // The first read runs the closure, the next ones reuse the value echo laze::read('DB_DSN'), "\n"; echo laze::read('DB_DSN'), "\n";
Defined. Evaluated yet? no (reading the configuration) pgsql:host=db;dbname=shop pgsql:host=db;dbname=shop
Immutable, after the first read
Before it, a definition can be replaced, which is how a test swaps in a mock. After it, the value stays.
<?php require __DIR__ . '/../vendor/autoload.php'; use divengine\laze; laze::define('GREETING', fn () => 'Hello'); // Before the first read, a value can still be replaced (a test can mock it) laze::define('GREETING', fn () => 'Hi'); echo laze::read('GREETING'), "\n"; // After it, the value is fixed: a new definition is ignored laze::define('GREETING', fn () => 'Bye'); echo laze::read('GREETING'), "\n";
Hi Hi
Constraints
A failed constraint throws and stores nothing, so the value can be defined again.
<?php require __DIR__ . '/../vendor/autoload.php'; use divengine\laze; // A constraint checks every value when it is evaluated laze::constraint('APP_PORT must be an integer', function ($key, $value) { return $key !== 'APP_PORT' || is_int($value); }); // The port comes from a text file, so it is a string: the read fails laze::define('APP_PORT', fn () => '8080'); try { echo laze::read('APP_PORT'), "\n"; } catch (Exception $e) { echo $e->getMessage(), "\n"; } // Nothing was stored, so the value can be defined again, correctly laze::define('APP_PORT', fn () => (int) '8080'); echo laze::read('APP_PORT'), "\n";
Constraint 'APP_PORT must be an integer' failed for lazy immutable value: APP_PORT 8080
Values made of values
A value can read others, defined before or after it, and a value can itself be a function.
<?php require __DIR__ . '/../vendor/autoload.php'; use divengine\laze; // Values can be built from other values, in any order of definition laze::define('GREETING', fn () => laze::read('FORMAT')(laze::read('NAME'))); laze::define('NAME', fn () => 'Ada'); // A value can be a function: here, the closure returns a closure laze::define('FORMAT', fn () => fn (string $name) => "Welcome, $name!"); echo laze::read('GREETING'), "\n";
Welcome, Ada!
Install
Add it to a project
1. Require it
With Composer.
composer require divengine/laze:1.1.1
2. Run an example
Save one of the scripts above in an examples folder of the project, then:
php examples/lazy.php
Values live for one PHP process. In tests, run each test in its own process (phpunit --process-isolation) so values do not leak between them. The examples, with the commands to run them again with Laze 1.1.1, are in the site's repository.