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?
Take a look at the PHP magic methods. You can use __get to get a value from your data storage (for example array) when an inaccessible property is called, and __set when it's set. The __call method 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With