Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Class auto-adjusting functions?

Tags:

php

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?

like image 548
Nathan Avatar asked Feb 26 '26 04:02

Nathan


1 Answers

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.

like image 194
Frog Avatar answered Mar 04 '26 15:03

Frog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!