Hey guys. I was designing a model for some data, and wanted it to work like: $this->groupmodel->VARIABLE->FUNCTION(VAR1, VAR2); to call a function, where VARIABLE is changeable to anything, and passed to the function.
This feels more correct (then say $this->groupmodel->FUNCTION(VARIABLE, VAR1, VAR2)), because each VARIABLE has the exact same functions, and the functions are being preformed (technically) on VARIABLE. Is this possible?
Note that VARIABLE can be set anywhere (in its own function or in the function being called) (it is persistent throughout the class, but needs to be set each call).
Max
You should create a class implementing the functions you want to use, and all your “variables” should be objects of that class. For instance:
And then inside your groupmodel:
so you can call:
What we are doing here is creating objects of class Kid automatically every time you try to set a property of the GroupModel object. (This is what the magic method _set() does)
In fact it creates them as elements of a private array instead of real properties of GroupModel.
Then, when trying to access those “properties”, _get() will be invoked and it will retrieve the element of the array and return it.
As it will be an object of class Kid, you could call every method it implements (like birthday()).
For more information on Overloading and magic methods like _get and _set, see:
http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members