1.2.73. Avoid get_object_vars()

get_object_vars() changes behavior between PHP 7.3 and 7.4. It also behaves different within and outside a class.

<?php

// Illustration courtesy of Doug Bierer
$obj = new ArrayObject(['A' => 1,'B' => 2,'C' => 3]);
var_dump($obj->getArrayCopy());
var_dump(get_object_vars($obj));

?>

See also get_object_vars script on 3V4L and The Strange Case of ArrayObject.

1.2.73.1. Suggestions

  • Use ArrayObject and getArrayCopy() method

1.2.73.2. Specs

Short name

Php/AvoidGetobjectVars

Rulesets

All, Changed Behavior, CompatibilityPHP74, CompatibilityPHP80

Exakat since

2.2.1

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

class, arrayobject

Available in

Entreprise Edition, Exakat Cloud