PHP Indirect modification of overloaded property - With ARRAYS -


there lot of questions error, have yet find correct solution. mine strictly php itself, not framework others have posted examples.

i trying overload __set , __get dynamically create these properties within data array, however, when try create property in examples below, receive error. can create properties if string or int, dont understand why won't pass 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 generate array field. (this cases error)

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

this works fine, not asked for, want fields index array.

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

if else has better way if impossible, i'd love know alternative same results. guys!

modify __get magic method return reference property stored in array. notice saying getting copy of $this->data[fields] value , appending it. appending copy of fields variable, not actual fields stored in class. have no effect on variable stored in class.

you should either throw exception when try key doesn't exist (similar notice undefined variable) or create index , return reference that. example uses 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


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -