website/errors/offsetAccess.nonArray.md
<?php declare(strict_types = 1);
function doFoo(stdClass $obj): void
{
[$a, $b] = $obj; // error: Cannot use array destructuring on stdClass.
}
Array destructuring ([$a, $b] = ...) requires the right-hand side to be an array or an object implementing ArrayAccess. When the expression being destructured is neither of those types, PHP cannot access its elements by offset, which will result in an error at runtime.
Ensure the value being destructured is an array.
-function doFoo(stdClass $obj): void
+function doFoo(array $data): void
{
- [$a, $b] = $obj;
+ [$a, $b] = $data;
}
If the value can sometimes be null, check for it before destructuring.
function doFoo(?array $data): void
{
- [$a, $b] = $data;
+ if ($data !== null) {
+ [$a, $b] = $data;
+ }
}