1.2.826. No Object As Index

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

Thanks to Gina 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.826.2. Connex PHP features

1.2.826.2.1. Suggestions

  • Filter values being used as index

  • Filter values being used as array

1.2.826.2.2. Specs

Short name

Structures/NoObjectAsIndex

Rulesets

All, Analyze, Changed Behavior

Exakat since

2.2.2

PHP Version

With PHP 8.1 and older

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Medium

Available in

Entreprise Edition, Exakat Cloud