AR - get value - performence

Why this is so?

The difference is sometimes more sometimes less, but the first option is always faster…

$ar = new \yii\db\ActiveRecord();

$ar->id = 123456789;

$numberOfIteration = 10000000; //10 millions

$tmp = null;

for ($i=0;$i<$numberOfIteration;$i++){

    $tmp = $ar->id; // faster: 25.631805896759 s


$tmp = null;

for ($i=0;$i<$numberOfIteration;$i++){

    $tmp = $ar['id']; // slower: 38.955304861069 ms


Who is $p ?

When you access an object property as an array it requires the use of another piece of code, that is the ArrayAccess interface. This needs to make some processing to allow access like an array property.


Also it depends how you get the data from the DB.