I am attempting to write a "listener" on a variable in a class I cannot modify. I am extending the class in question, unsetting the properties I want to listen to, and then using __set to intercept writes to that variable. At that point I compare with the previous version and report if there are changes.
class A {
var $variable;
...
}
class B extends A {
var $new_variable
function __construct() {
parent::__construct();
unset($this->variable);
}
function __set($thing, $data) {
if ($thing == 'variable') {
// Report change
// Set $new_variable so we can use __get on it
}
}
public function __get($var) {
if (isset($this->$var)) {
// Get as normal.
return $this->$var;
} elseif ($var == 'variable' && isset($this->new_variable)) {
return $this->new_variable;
}
}
...
}
This works if I modify the class in question directly instead of via an extended class, removing the declaration of the variable and introducing the setter and getter methods. The problem is when I use the pattern shown above, the unset() call does not seem to actually remove the variables inherited from the parent class, thus rendering the __set method unable to intercept the variable's value.
So far this seems to me to be the only way I can watch the variables changes, but I do not want to hack the core of the framework, only inspect it's handy work (a parser). Is there a possibility of making this work, or another way to approach this problem?