indirect modification of overloaded property

Note I am using this php version: PHP 5.3.5-1ubuntu7.2 with Suhosin-Patch (cli) (built: May 2 2011 23:00:17) Copyright © 1997-2009 The PHP Group

I got this exception: "indirect modification of overloaded property"

when I tried this:

$this->models[$class]->attributes = <some array returned from function>;

Note: The above code worked perfectly. Although I moved the $models property inside a Behavior

After googling I think the problem is that I am referring to a property as an array while the property does not exist (this is true, it should not exist, now is part of the behavior)

I temporarily solved the problem using this code:

$models = $this->models;

$models[$class]->attributes = <some array returned from function>;

$model->models = $models;

Note: Yes indeed I have implemented the __get and __set php special methods inside the behavior. I am not exactly sure if using __get and __set methods inside a behavior is a neat implementation. Please tell me if this hides problematic issues :)

This used to be a php bug as I see in other forums (other websites)

So is there a way to solve the above problem without changing the code in every class that uses this behavior?

Thank you!

The same error message happened to me when I was trying to set the CActiveRecord Attributes array.

So I discovered another method to overcome this issue, in a case where the magic method is related to an object variable which contains an array take a look: you create an AUXILIARY ARRAY in which you put the original and the new values (sometimes one wants to REPLACE a value related to one of the keys, and these methods are not satisfactory). And AFTERWARDS use an assignation, which works like the reference. For example, in the next example, I generalize $model->attribute to

"Object->array_built_with_magic", but I used it first with $model->attribute. Take a look:

$auxiliary_array = array();

foreach(Object->array_built_with_magic as $key=>$value) {

if(….) {

$auxiliary_array[$key] = Object->array_built_with_magic[$key];

} else if (…) {

$auxiliary_array[$key] = $NEW_VALUE



//So now we have the array $auxiliary_array with the

// desired MIX (that is, some originals, some modifications)

//So we will do now:

Object->array_built_with_magic =$auxiliary_array;

I hope it’s useful, regards.

Note: please moderators RESTRAIN from saying “this is an old thread”. It’s quite common to find new solutions after several years from an old problem.

David López

Investigación y Programación