How can I remove a property from a parent object in an extended class?
I am trying to write a "listener" for a variable in a class that I cannot change. I am extending the class in question by disabling the properties that I want to listen to and then using __set to intercept the entries into that variable. At this point, I compare with the previous version and report if there are any 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, rather than via the extended class, by removing the variable declaration and injecting setter and getter methods. The problem is that when I use the pattern shown above, calling unset () does not actually remove the variables inherited from the parent class, making the __set method unable to intercept the variable's value.
So far, it seems to me that this is the only way to observe changes in variables, but I do not want to break into the core of the structure, but only check its convenient operation (parser). Is there a way to make this work, or is there another way to approach this problem?
source to share
Mmm, this is weird. The following code works fine:
class A
{
var $variable;
}
class B extends A
{
var $new_variable;
function __construct()
{
unset($this->variable);
}
function __set($thing, $data)
{
if ($thing == 'variable')
{
echo "\nThe variable value is '" . $data . "'\n";
}
}
}
$b = new B();
$b->variable = 'Intercepted'; //Output: The variable value is 'Intercepted'
$b->new_variable = 'Not intercepted'; // No output
Can you tell me that this piece of code does what you need, and if it doesn't, what you need?
NTN
source to share