Is it possible to dynamically add data to PHP?

I am wondering if it is possible to add new class data members at runtime in PHP?

+3
source share
2 answers

Yes.

$prop = 'newname';
$obj->$prop = 42;

will do the same thing:

$obj->newname = 42;

Any of these will add "newname" as a property in $ obj if it does not already exist.

+11
source

It. You can add public members, run-time without additional code, and can affect protected / private members using the magic __get () / __ set () overload methods. See here for more details .

+2
source

Source: https://habr.com/ru/post/1697837/


All Articles