Can I call a method when an object's property value changes?
Posted: Tue Mar 23, 2010 3:21 am
I'm looking for an alternative to this:
Instead of forcing the value of $myProperty to be changed via the setter method (thus calling the someMethod function on the value) is there another manner to do this? Something that would be syntactically similar to the following?
Any ideas, feedback.. anything is appreciated
(This falls into the category of "overloading", does it not?)
Code: Select all
class MyClass{
private $myProperty;
private function someMethod($val){
//do something to $val
return $val;
}
public function setMyProperty($val){
if(isset($val)){
$this->myProperty = self::someMethod($val);
}else{
return false;
}
}
Code: Select all
class MyClass{
public $myProperty;
private function someMethod($val){
//do something to $val
return $val;
}
public function myProperty(){ //called whenever the value of $myProperty is changed
$this->myProperty = self::someMethod($this->myProperty);
}
}