1. Go to this page and download the library: Download divengine/laze library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
divengine / laze example snippets
use divengine\laze;
// Define a lazy constant
laze::define('MY_CONSTANT', fn() => computeExpensiveValue());
$value = laze::read('MY_CONSTANT');
use divengine\laze;
// Define a lazy constant. Simulate an expensive computation
laze::define('MY_CONSTANT', fn() => return rand(1, 100));
// First access, the closure is evaluated
$value = laze::read('MY_CONSTANT');
echo $value; // Outputs the evaluated value
// Subsequent access returns the stored value
$value = laze::read('MY_CONSTANT');
echo $value; // Outputs the same value as before
interface Configurable {
public function configure(array $settings): void;
}
class AppConfig implements Configurable {
private array $settings;
public function configure(array $settings): void {
$this->settings = $settings;
}
public function getSetting(string $key) {
return $this->settings[$key] ?? null;
}
}
// 1. Add a constraint to ensure a value implements the Configurable interface
laze::constraint(
name: 'Must implement Configurable interface',
fn($key, $value) => $key == 'APP_CONFIG' ? $value instanceof Configurable : true
);
// 2. Define a lazy value that returns a closure
laze::define('MY_FUNCTION', function() {
return function() {
return "Function Result";
};
});
// 3. Define a lazy value with an object instance
laze::define('APP_CONFIG', function() {
$config = new AppConfig();
$config->configure([
'timezone' => 'UTC',
'locale' => 'en_US'
]);
return $config;
});
// 4. Reuse define and read within each other
laze::define('FINAL_MESSAGE', function() {
$config = laze::read('APP_CONFIG');
$timezone = $config->getSetting('timezone');
return laze::read('MY_FUNCTION')() . " in timezone $timezone";
});
$finalMessage = laze::read('FINAL_MESSAGE');
echo $finalMessage; // Outputs: "Function Result in timezone UTC"
// 5. PHPUnit Test - Redefining a value
class LazeTest extends \PHPUnit\Framework\TestCase {
public function testAppConfigCanBeMocked() {
// mock function
laze::define('MY_FUNCTION', function() {
return function() {
return "Mocked Result";
};
});
// mock object
laze::define('APP_CONFIG', function() {
$mockConfig = $this->createMock(Configurable::class);
$mockConfig->method('getSetting')->willReturn('mocked_timezone');
return $mockConfig;
});
$message = laze::read('FINAL_MESSAGE');
$this->assertEquals("Mocked Result in timezone mocked_timezone", $message);
}
}
class LazeTest extends \PHPUnit\Framework\TestCase {
public $lazeBackup;
public function setUp(): void {
parent::setUp();
$reflection = new ReflectionClass(laze::class);
$property = $reflection->getProperty('store');
$property->setAccessible(true);
$this->lazeBackup = $property->getValue();
}
public function tearDown(): void {
$reflection = new ReflectionClass(laze::class);
$property = $reflection->getProperty('store');
$property->setAccessible(true);
$property->setValue($this->lazeBackup);
parent::tearDown();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.