php - How can I delete a property from a parent object in an extended class? -
php - How can I delete a property from a parent object in an extended class? -
i attempting write "listener" on variable in class cannot modify. extending class in question, unsetting properties want hear to, , using __set intercept writes variable. @ point compare previous version , study if there changes.
class { var $variable; ... } class b extends { var $new_variable function __construct() { parent::__construct(); unset($this->variable); } function __set($thing, $data) { if ($thing == 'variable') { // study alter // set $new_variable can utilize __get on } } public function __get($var) { if (isset($this->$var)) { // normal. homecoming $this->$var; } elseif ($var == 'variable' && isset($this->new_variable)) { homecoming $this->new_variable; } } ... }
this works if modify class in question straight instead of via extended class, removing declaration of variable , introducing setter , getter methods. problem when utilize pattern shown above, unset() phone call not seem remove variables inherited parent class, rendering __set method unable intercept variable's value.
so far seems me way can watch variables changes, not want hack core of framework, inspect it's handy work (a parser). there possibility of making work, or way approach problem?
mmm, weird. next code works fine:
class { var $variable; } class b extends { var $new_variable; function __construct() { unset($this->variable); } function __set($thing, $data) { if ($thing == 'variable') { echo "\nthe variable value '" . $data . "'\n"; } } } $b = new b(); $b->variable = 'intercepted'; //output: variable value 'intercepted' $b->new_variable = 'not intercepted'; // no output
can tell me if piece of code need and, if doesn't, need instead?
hth
php inheritance unset
Comments
Post a Comment