tags:

views:

117

answers:

1

Hi folks!

When I use call_user_func on a non-static method in PHP 5.2 I get a Strict Warning:

Strict Standards: Non-static method User::register() cannot be called statically

But on PHP 5.3.1 I don't get this warning. Is this a bug in PHP 5.3.1 or is the warning removed?

+4  A: 

It is perfectly OK -- but note that you have to pass an object that's an instance of your class, to indicate on which object the non-static method shall be called :

class MyClass {
    public function hello() {
        echo "Hello, World!";
    }
}

$a = new MyClass();
call_user_func(array($a, 'hello'));


You should not use something like this :

call_user_func('MyClass::hello');

Which will give you the following warning :

Strict standards: `call_user_func()` expects parameter 1 to be a valid callback,
non-static method `MyClass::hello()` should not be called statically 

(This would work perfectly fine if the method was declared as static... but it's not, here)


For more informations, you can take a look at the callback section of the manual, which states, amongst other things (quoting) :

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.


If you get a strict error with PHP 5.2 and not with PHP 5.3, it's probably a matter of configuration -- I'm thinking about the error_reporting directive.

Note that E_ALL doesn't include E_STRICT (quoting) :

E_ALL : All errors and warnings, as supported, except of level E_STRICT.

Pascal MARTIN
E_STRICT is supposed to be included in E_ALL in PHP 5.3 I think. Btw thank you, I instantiated the object and then it works just fine.
sandelius
OK about instanciating the object :-) ;;; I don't think E_STRICT is included in E_ALL, even in PHP 5.3 -- I suppose it would be said so in the manual ;-)
Pascal MARTIN