I have this little helper method here:
private function continueConv($attribute, $next): void
{
$sequence = [ $this->askName(),
$this->askEmail(),
$this->askPhone(),
$this->manageMessage()
];
match($attribute) {
'user_name' => $sequence[0 (int)$next],
'user_email' => $sequence[1 (int)$next],
'user_phone' => $sequence[2 (int)$next]
};
return;
}
When the interpreter evaluates the $sequence array, instead of just storing the pointers to those functions, it calls them all! Is this a bug? Are there working alternatives? Thanks
EDIT: alternative approach that works:
private function continueConv($attribute, $next): void
{
match($attribute) {
'user_name' => $next ? $this->askEmail() : $this->askName(),
'user_email' => $next ? $this->askPhone() : $this->askEmail(),
'user_phone' => $next ? $this->manageMessage() : $this->askPhone()
};
return;
}
but I'd prefer to use an array of functions in order to easily change the sequence.
CodePudding user response:
You can use an array containing the object and method name as a callable. So just put the method names in the array.
private function continueConv($attribute, $next): void
{
$sequence = ['askName', 'askEmail', 'askPhone', 'manageMessage'];
$func = [$this, match($attribute) {
'user_name' => $sequence[0 $next],
'user_email' => $sequence[1 $next],
'user_phone' => $sequence[2 $next]
}];
$func();
}