Php run method on property change

Like @lucas said, if you can set your property as private within the class, you can then use the __set() to detect a change.

class MyClass
{
  private $MyProperty;

  function __set($name, $value)
  {
     if(property_exists('MyClass', $name)){
       echo "Property". $name . " modified";
     }
  }

 }


$r = new MyClass;
$r->MyProperty = 1; //Property MyProperty changed.

You can achieve this best by using a setter method.

class MyClass
{
    private MyProperty;

    public function setMyProperty($value)
    {
        $this->MyProperty = $value;
        echo "MyProperty changed to " . $this -> MyProperty;
    }

}

Now you simply call the setter instead of setting the value yourself.

$MyObject = new MyClass;
$MyObject -> setMyProperty(1);