tmarois
tmarois

Reputation: 2470

PHP Indirect modification of overloaded property - With ARRAYS

There will be a lot of questions with this error, I have yet to find the correct solution. Mine is strictly with PHP itself, not a framework as others have posted examples.

I am trying to overload with __set and __get to dynamically create these properties within the data array, however, when I try to create a property with like in the examples below, I receive this error. But I can create the properties if it was a string or int, so I dont understand why it won't pass the value?

Indirect modification of overloaded property ::fields has no effect

Class getter method:

public function __get($name)
{
    if (array_key_exists($name, $this->data))
    {
        return $this->data[$name];
    }
}

Trying to generate an array field. (this cases the error)

$fieldset->fields[] = [
    'handle'      => 'first_name',
    'name'        => 'First Name',
];

This works just fine, but its not what I asked for, I want fields to be an index array.

$fieldset->fields = [
    'handle'      => 'first_name',
    'name'        => 'First Name',
];

If anyone else has a better way if this is impossible, I'd love to know an alternative to the same results. Thanks guys!

Upvotes: 4

Views: 8120

Answers (1)

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

Modify your __get magic method to return a reference to the property stored in the array. The notice is saying that you are getting a copy of $this->data[fields] by value and appending something to it. You are appending to a copy of the fields variable, not the actual fields stored in the class. Which would have no effect on the variable stored in the class.

You should also either throw an exception when you try to get a key that doesn't exist (similar to the notice undefined variable) or create an index and return the reference to that. This example uses the latter.

Example:

public function &__get($name)
{
    if (!array_key_exists($name, $this->data))
    {
        $this->data[$name] = null;
    }
    return $this->data[$name];
}

Demo: https://3v4l.org/sdIWH

Upvotes: 10

Related Questions