9

Is it possible to run php method on property change? Like shown in the example below:

Class:

class MyClass
{
    public MyProperty;

    function __onchange($this -> MyProperty)
    {
        echo "MyProperty changed to " . $this -> MyProperty;
    }        
}

Object:

$MyObject = new MyClass;
$MyObject -> MyProperty = 1;

Result:

 "MyProperty changed to 1"
3
  • 2
    Put the variable as private and use a function to change it. Commented Oct 27, 2015 at 11:59
  • You'll have to implement it. E.g Change the property, trigger the event and capture the event. Commented Oct 27, 2015 at 12:01
  • Of interest: stackoverflow.com/a/17543256/3303195 Commented May 15, 2018 at 23:52

4 Answers 4

5

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Good one but affects all private properties
It's not called when changing a property from within the class. Any ideas on how to do such thing?
3

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);

Comments

0

Using the magic method __set:

class Foo {

    protected $bar;

    public function __set($name, $value) {
        echo "$name changed to $value";
        $this->$name = $value;
    }

}

$f = new Foo;
$f->bar = 'baz';

It may be a better and more conventional idea to use a good old-fashioned setter instead:

class Foo {

    protected $bar;

    public function setBar($value) {
        echo "bar changed to $value";
        $this->bar = $value;
    }

}

$f = new Foo;
$f->setBar('baz');

Comments

-1

This post is quite old, but I have to deal with the same issue and a typical class with multiple fields. Why using private properties ? Here is what I implemented :

class MyClass {

    public $foo;
    public $bar;

    function __set($name, $value) {
        if(!property_exists('MyClass', $name)){ // security
            return;
        }
        if ('foo' == $name) { // test on the property needed to avoid firing 
                              // "change event" on every private properties
            echo "Property " . $name . " modified";
        }
        $this->$name = $value;
    }

}

$r = new MyClass();
$r->bar = 12;
$r->foo = 1; //Property foo changed.
var_dump($r);
/*
object(MyClass)[1]
  public 'foo' => int 1
  public 'bar' => int 12
*/

1 Comment

This won't work since the foo property has to be either protected or private for the setter to be called. If public it will just change the value and bypass the __set method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.