.. _classes-nomagicwitharray: .. _no-magic-method-with-array: No Magic Method With Array ++++++++++++++++++++++++++ Magic method ``__set()`` doesn't work for array syntax. When overloading properties, they can only be used for scalar values, excluding arrays. Under the hood, PHP uses ``__get()`` to reach for the name of the property, and doesn't recognize the following index as an array. It yields an `error `_ : "Indirect modification of overloaded property". It is possible to use the array syntax with a magic property : by making the ``__get`` returns an array, the syntax will actually extract the expected item in the array. This is not reported by linting. In this analysis, only properties that are found to be magic are reported. For example, using the b property outside the class scope is not reported, as it would yield too many false-positives. .. code-block:: php o[$name]; } function foo() { // property b doesn't exists $this->b['a'] = 3; print_r($this); } // This method has no impact on the issue function __set($name, $value) { $this->o[$name] = $value; } } $c = new c(); $c->foo(); ?> See also `Overload `_. Related PHP errors ------------------- + `0 `_ Connex PHP features ------------------- + `magic-method `_ Suggestions ___________ * Use a distinct method to append a new value to that property * Assign the whole array, and not just one of its elements Specs _____ +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Short name | Classes/NoMagicWithArray | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Rulesets | :ref:`All `, :ref:`Analyze `, :ref:`CE `, :ref:`CI-checks `, :ref:`Changed Behavior `, :ref:`LintButWontExec ` | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Exakat since | 0.12.4 | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | PHP Version | All | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Severity | Major | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Time To Fix | Slow (1 hour) | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Precision | Medium | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Note | This issue may lint but will not run | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Available in | `Entreprise Edition `_, `Community Edition `_, `Exakat Cloud `_ | +--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+