Simone Nigro Simone Nigro - 10 months ago 39
PHP Question

PHP call_user_func use reference on &$this

what is the best way (memory safe) to use

from self instance eg.:

call_user_func(array($this, 'method'));

or by reference, eg.:

call_user_func(array(&$this, 'method'));

PS. i know the best way is
but for framework design pattern reason i do use


If I am aware, it doesn't really matter, because PHP will do the same thing in both cases: will just pass reference to the same object (the object will be not cloned). So for simplicity, I would go without &

Some test:

class test 
    function foo() {
        call_user_func([$this, 'bar']); 
        call_user_func([&$this, 'bar']);
    private function bar() {
        echo spl_object_hash($this), PHP_EOL;

and outputs:

php > $obj = new test();
php > $obj->foo();