Hi,
I have a question in regards to the proposed PHP RFC: Records. For
following snippet:
record Result(bool $success, array $error, array $data);
$result = &Result(false, [], []);
$result->with(success: true)->with(error: $error + ['Some
error'])->with(data:
$data + ['user_id' => 777]));
Is this possible to append/merge to existing array properties? Like I am
doing with error.
Regards
Volodymyr Volynets
Hi,
I have a question in regards to the proposed PHP RFC: Records. For following snippet:
record Result(bool $success, array $error, array $data);
$result = &Result(false, [], []);
$result->with(success: true)->with(error: $error + ['Some error'])->with(data: $data + ['user_id' => 777]));Is this possible to append/merge to existing array properties? Like I am doing with error.
Regards
Volodymyr Volynets
Hello Volodymyr,
Assuming you mean $data and $error to be the record's $data and $error arrays respectively? In that case, it would have to be something like this:
$result = $result->with(
success: true,
error: $result->error + ['some error'],
data: $result->data + ['user_id' => 777],
);
This is akin to writing it with an array:
$result = ['success' => false, 'error' => [], 'data' => []];
$result = [
...$result,
'success' => true,
'error' => $result['error'] + ['some error'],
'data' => $result['data'] + ['user_id' => 777],
];
— Rob