website/errors/offsetAccess.nonOffsetAccessible.md
<?php declare(strict_types = 1);
function doFoo(int $value): void
{
echo $value[42];
}
The code attempts to use array access syntax ($var[...] or $var[]) on a type that does not support offset access. Types like int, float, bool, resource, stdClass, and Closure do not support the [] operator.
Only arrays, strings, and objects implementing ArrayAccess support offset access in PHP.
Fix the type of the variable so it supports offset access:
-function doFoo(int $value): void
+function doFoo(array $value): void
{
echo $value[42];
}
Or fix the access to use a method appropriate for the type:
function doFoo(stdClass $value): void
{
- echo $value['foo'];
+ echo $value->foo;
}
If the variable might or might not be an array, narrow the type before accessing it:
<?php declare(strict_types = 1);
function doFoo(array|int $value): void
{
if (is_array($value)) {
echo $value[42];
}
}