<?php
class SwitchStage{
function __construct(
public array $branches = [],
public ?int $default = null
) {}
function branch(int $case, int $then) {
$this->branches[] = (object) ['case' => $case, 'then' => $then];
return $this;
}
function default(int $default) {
$this->default = $default;
return $this;
}
}
$x = (new SwitchStage())
->branch(case: 1, then: 2)
->branch(case: 3, then: 4)
->default(5);
var_dump($x);
$x = new SwitchStage([
['case' => 1, 'then' => 2],
['case' => 3, 'then' => 4],
], default: 5);
var_dump($x);
- Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- object(SwitchStage)#1 (2) {
["branches"]=>
array(2) {
[0]=>
object(stdClass)#2 (2) {
["case"]=>
int(1)
["then"]=>
int(2)
}
[1]=>
object(stdClass)#3 (2) {
["case"]=>
int(3)
["then"]=>
int(4)
}
}
["default"]=>
int(5)
}
object(SwitchStage)#4 (2) {
["branches"]=>
array(2) {
[0]=>
array(2) {
["case"]=>
int(1)
["then"]=>
int(2)
}
[1]=>
array(2) {
["case"]=>
int(3)
["then"]=>
int(4)
}
}
["default"]=>
int(5)
}
preferences:
80.25 ms | 408 KiB | 5 Q