PHP how to detect the change of variable?
php
Solution
know that it is easy to achieve if I use the "setter" method. But since I am working on some existing codes, I am not able to modify them.
I assume that you can change some code, but not the object / class you are working with. If you cannot change any code at all this question would be useless.
What you can do is make your own class, extending the class you are working with, and adding your setter there. For all purposes you can not-override the parent setting, except for a magic setter on whatever you need to track. Track changes and then call the parent functions, so no changes in any other internal workings will be in effect.
Problem
Is PHP exists a function that detect the change of variable? That is something like this: ``` //called when $a is changed. function variableChanged($value) { echo "value changed to " . $value; } $a = 1; //attach the variable to the method. $a.attachTo("variableChanged"); $a = 2; $a = 3; //expected output: //value changed to 2 //value changed to 3 ``` I know that it is easy to achieve if I use the "setter" method. But since I am working on some existing codes, I am not able to modify them. Can somebody tell me how to achieve my purpose? Thanks.