Supported Versions: PHP 4, PHP 5, PHP 7, PHP 8
Generate a random integer
<?php rand(): int
<?php {
public function rand()
{
return once(fn () => rand(1, PHP_INT_MAX));
}
};
<?php public function rand(string $letter)
{
return once(function () use ($letter) {
return $letter.rand(1, 10000000);
});
}
};
<?php $letter = 'a';
a:
$results[] = once(fn () => $letter.rand(1, 10000000));
if (count($results) < 2) {
goto a;
<?php public function testMemoizationWhenOnceIsWithinClosure()
{
$resolver = fn () => once(fn () => rand(1, PHP_INT_MAX));
$first = $resolver();
$second = $resolver();
<?php {
$this->markTestSkipped('This test shows a limitation of the current implementation.');
$result = [once(fn () => rand(1, PHP_INT_MAX)), once(fn () => rand(1, PHP_INT_MAX))];
$this->assertNotSame($result[0], $result[1]);
}
<?php public function testResultIsDifferentWhenCalledFromDifferentClosures()
{
$resolver = fn () => once(fn () => rand(1, PHP_INT_MAX));
$resolver2 = fn () => once(fn () => rand(1, PHP_INT_MAX));
$first = $resolver();
<?php public function testResultIsDifferentWhenCalledFromDifferentClosures()
{
$resolver = fn () => once(fn () => rand(1, PHP_INT_MAX));
$resolver2 = fn () => once(fn () => rand(1, PHP_INT_MAX));
$first = $resolver();
$second = $resolver2();
<?php {
public function rand()
{
return once(fn () => rand(1, PHP_INT_MAX));
}
};
<?php {
public function rand()
{
return once(fn () => rand(1, PHP_INT_MAX));
}
};
<?php {
public function rand()
{
return once(fn () => once(fn () => rand(1, PHP_INT_MAX)));
}
};
<?php $letter = 'a';
$GLOBALS['onceable1'] = fn () => once(fn () => $letter.rand(1, PHP_INT_MAX));
$GLOBALS['onceable2'] = fn () => once(fn () => $letter.rand(1, PHP_INT_MAX));
function my_rand()
<?php $letter = 'a';
$GLOBALS['onceable1'] = fn () => once(fn () => $letter.rand(1, PHP_INT_MAX));
$GLOBALS['onceable2'] = fn () => once(fn () => $letter.rand(1, PHP_INT_MAX));
function my_rand()
{
<?php function my_rand()
{
return once(fn () => rand(1, PHP_INT_MAX));
}
class MyClass
<?php {
public function rand()
{
return once(fn () => rand(1, PHP_INT_MAX));
}
public static function staticRand()
<?php public static function staticRand()
{
return once(fn () => rand(1, PHP_INT_MAX));
}
public function callRand()