1

I have an object named $obj. I have overridden the __call function for that class, so that when I call $obj->setVariableName($value) then this happens: $obj->variableName = $value. I don't know when and how exactly $obj->setVariableName($value) is called in the project. So, during running the application this happens:

setVariable1($value) : works!
setVariable2($value) : works!
setVariable3($value) : It won't trigger __call()
setVariable4($value) : works!

And when I write the extra function setVariable3, then it works. I don't know how setVariable3 is called, whether it is called directly by $obj->setVariable3 or it is called with a function like call_user_func_array.

What the problem might be that __call is not working for setVariable3?

Update: Now I know that setVariable3 is called from a $form->bind($user) and running $user->setVariable3('foo') works. (This is a ZF2+Doctrine project)

Mostafa Shahverdy
  • 2,687
  • 2
  • 30
  • 51
  • It can not be so. Also, why are you using `__call()` in such strange way? Setters (so `__set()`) is exactly the feature that you're looking for – Alma Do Mar 05 '14 at 10:11
  • `__set` is for the time that I want to run `$obj->variableName = $value`. In this case I don't have access to how its called. In fact in this case I know that somewhere in the code `$obj->setVariable3($value)` is called, and I need to define the function for it. – Mostafa Shahverdy Mar 05 '14 at 10:16
  • As you are unclear on where/how the variable is being set, the first port of call might be to investigate the calling code. To help you identify the calling code use `debug_backtrace` function. See - http://bd1.php.net/debug_backtrace. – Kami Mar 05 '14 at 10:23
  • `var_dump(debug_backtrace());` gave me a huge amount of data, I'm using ZF2+Doctrine – Mostafa Shahverdy Mar 05 '14 at 10:33
  • @MostafaShahverdy you should output only the caller by filtering the data or only the levels you are interested in - http://stackoverflow.com/a/190426/1603275 – Kami Mar 05 '14 at 10:37

1 Answers1

0

Sounds strange but works for me.

class test {
    public function __call($method, $args)
    {
        printf("Called %s->%s(%s)\n", __CLASS__, __FUNCTION__, json_encode($args));
    }
}
$test = new test();
$value = 'test';
$test->setVariable1($value);
$test->setVariable2($value);
$test->setVariable3($value);
$test->setVariable4($value);

Will output:

Called test->__call(["test"])
Called test->__call(["test"]) 
Called test->__call(["test"]) 
Called test->__call(["test"])

And __set will only be called if you try to access unaccessible properties. e.g.

class test {
    public function __set($var, $value) {
        printf("Called %s->%s(%s)\n", __CLASS__, __FUNCTION__, json_encode(func_get_args()));
    }
}
$test = new test();
$value = 'test';
$test->callMagicSet = $value;

will result in:

Called test->__set(["callMagicSet","test"])
Tobias
  • 81
  • 3