Error control operator
In case the warning is expected you can use the error control operator @
to suppress thrown messages.
$role_arr = getRole(@$response->records);
While this reduces clutter in your code you should use it with caution as it may make debugging future errors harder. An example where using @
may be useful is when creating an object from user input and running it through a validation method before using it in further logic.
Null Coalesce Operator
Another alternative is using the isset_ternary operator ??
. This allows you to avoid warnings and assign default value in a short one line fashion.
$role_arr = getRole($response->records ?? null);