PHP Indirect modification of overloaded property - With ARRAYS

Viewed 5157

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!

1 Answers
Related