1

Lets say I have the following abstract class:

abstract class A{
    abstract public function foo(){}
}

And this child class that extends the above abstract class:

class B extends A{
    public function foo(){ echo 'bar'; }
}

And somewhere else in my code, I call:

call_user_func( array('B', 'foo') );

As far as I can tell from the call_user_func() documentation, foo() will be called statically:

Class methods may also be invoked statically using this function by passing array($classname, $methodname) to this parameter. Additionally class methods of an object instance may be called by passing array($objectinstance, $methodname) to this parameter.

However, I was under the impression that since PHP 5.3+, abstract static methods were not allowed/should not be used. Is it that abstract static methods cannot be declared, but can still be called statically either using the :: operator or call_user_func()? I am confused ...

Community
  • 1
  • 1
rafiki_rafi
  • 1,177
  • 2
  • 11
  • 27
  • Yes. It calls the method with no errors. I'm just confused conceptually as far as on the one hand, abstract static methods should not be declared, but on the other, PHP allows you to invoke them. – rafiki_rafi Jan 13 '14 at 21:58
  • 1
    In PHP, *any* method can be called statically (as in many languages, there is no keyword requiring a function to be called non-statically); the fact that the method has an `abstract` parent makes no difference at all. Also, note the difference between "should not" and "can not" - "Strict Standards" is one of the mildest forms of warning, and [no version actually disallows `abstract static function`[(http://3v4l.org/E1E5Y). – IMSoP Jan 13 '14 at 22:02

1 Answers1

3

In PHP 5.5.5 I get:

Strict Standards: call_user_func() expects parameter 1 to be a valid callback, non-static method B::foo() should not be called statically in x line x

You can call it using an object of class B instead:

// this is static because you state a class B
call_user_func( array('B', 'foo') );

//this is non-static because you pass an instance of class B
call_user_func( array(new B, 'foo') );

Both display bar.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87