I’m writing a class that I’d like to be able to expand pretty easily, but each variable needs either 3 or 4 functions to go with it. I’m trying to think up a way I can utilize arrays or some other data type so that I can write the mandatory functions and use them with each variable without having to write more and more code, just simply add a new variable to the array and it’s handled automatically. Has anyone done anything like this?
Share
Take a look at the PHP magic methods. You can use
__getto get a value from your data storage (for example array) when an inaccessible property is called, and__setwhen it’s set. The__callmethod is called when an inaccessible method is called, so you can check for either one of you 4 needed methods there. Please note that magic methods are a bit slower than manually coding all methods.