- var_dump: documentation ( source)
- rand: documentation ( source)
<?php
interface StrategyInterface
{
public function run();
}
class Strategy implements StrategyInterface
{
protected ?string $value = null;
private static $counter = 0;
public function __construct(private Closure $closure)
{
echo "Strategy Counter: ", ++self::$counter, PHP_EOL;
$this->value = (string)rand(1, 100);
}
public function run()
{
$this->closure->call($this);
}
public function __destruct()
{
echo "Strategy Counter: ", --self::$counter, PHP_EOL;
}
}
$strategy1 = new Strategy(function () {
var_dump($this->value);
});
$strategy2 = new Strategy(function () {
var_dump($this->value);
});
$strategy3 = new Strategy(function () {
var_dump($this->value);
});
$strategy1->run();
$strategy1 = null;
$strategy2->run();
unset($strategy2);
$strategy3->run();
$strategy4 = new Strategy(function () {
var_dump($this->value);
});