1.2.820. No Object As Index

PHP accepts objects as index, though it will report various error messages when this happens.

Thanks to George Peter Banyard for the inspiration.

<?php

$s = 'Hello';
$o = new stdClass();

try {
    $s[$o] = 'A';
} catch (\Throwable $e) {
    echo $e->getMessage(), "\n";
    //Cannot access offset of type stdClass on string
}

?>

See also Use an object as an offet.

1.2.820.1. Suggestions

  • Filter values being used as index

  • Filter values being used as array

1.2.820.2. Specs

Short name

Structures/NoObjectAsIndex

Rulesets

All, Analyze

Exakat since

2.2.2

PHP Version

With PHP 8.1 and older

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Medium

Features

index

Available in

Entreprise Edition, Exakat Cloud