Reputation: 13975
I am wondering if there is a way to watch for variable changes in PHP. For example let's say I have a class
class Chicken
{
public $test = "hi";
}
$test = new Chicken();
$test->test = "hello"; // I want to know when this happens
Is there anyways to determine if a variable was changed?
Upvotes: 0
Views: 1008
Reputation: 219804
Make this a private variable and then use the __set()
magic method to filter all member variable value changes.
class Chicken
{
private $test = "hi";
public function __set($name, $value) {
echo "Set:$name to $value";
$this->$name = $value;
}
public function getTest() {
return $this->test;
}
// Alternative to creating getters for all private members
public function __get($name) {
if (isset($this->$name)) {
return $this->$name;
}
return false;
}
}
$test = new Chicken();
$test->test = "hello"; // prints: Set: test to hello
Upvotes: 9