views:

2287

answers:

4

After enabling strict warnings in PHP 5.2, I saw a load of strict standards warnings from a project that was originally written without strict warnings:

Strict Standards: Static function Program::getSelectSQL() should not be abstract in Program.class.inc

The function in question belongs to an abstract parent class Program and is declared abstract static because it should be implemented in its child classes, such as TVProgram.

I did find references to this change here:

Dropped abstract static class functions. Due to an oversight, PHP 5.0.x and 5.1.x allowed abstract static functions in classes. As of PHP 5.2.x, only interfaces can have them.

My question is: can someone explain in a clear way why there shouldn't be an abstract static function in PHP?

+10  A: 

static methods belong to the class that declared them. When extending the class, you may create a static method of the same name, but you are not in fact implementing a static abstract method.

Same goes for extending any class with static methods. If you extend that class and create a static method of the same signature, you are not actually overriding the superclass's static method

EDIT (Sept. 16th, 2009)
Update on this. Running PHP 5.3, I see abstract static is back, for good or ill. (see http://jp.php.net/lsb for more info)

Jonathan Fingland
OK, so what if I wanted to enforce the need for function getSelectSQL() in all children that extend my abstract class? getSelectSQL() in the parent class has no valid reason to exist. What's the best plan of action? The reason I chose abstract static is that the code wouldn't compile until I've implemented getSelectSQL() in all children.
Artem Russakovskii
Most likely, you should redesign things so getSelectSQL() is a abstract /instance/ method. That way, /instances/ of each child will have such a method.
Matthew Flaschen
that is probably not the best way to go about it. Essentially, you're trying to take advantage of polymorphism, which in php is very easy to abuse as types are not strictly enforced (new php 5.3 features aside). However, is what you're looking for really required to be static? is it related to the object you are passing or the whole class from which it is instantiated?
Jonathan Fingland
that last one was @Artem, not Matt. @Matt Completely agree
Jonathan Fingland
Abstract static is still disallowed in PHP 5.3. Late static bindings have nothing to do with it. See also http://stackoverflow.com/questions/2859633
Artefacto
A: 

I would argue that an abstract class/interface could be seen as a contract between programmers. It deals more with how things should look/ behave like and not implement actual functionality. As seen in php5.0 and 5.1.x it's not a natural law that prevents the php developers from doing it, but the urge to go along with other OO design patterns in other languages. Basically these ideas try to prevent unexpected behavior, if one is already familiar with other languages.

merkuro
OK, so what are you saying?..
Artem Russakovskii
Although not php related here is another good explanation: http://stackoverflow.com/questions/3284/why-cant-i-have-abstract-static-methods-in-c
merkuro
A: 

Look into PHP's 'Late Static Binding' issues. If you're putting static methods on abstract classes, you're probably going to run into it sooner rather than later. It makes sense that the strict warnings are telling you to avoid using broken language features.

Sean McSomething
A: 

I know this is old but....

Why not just throw an exception the that parent class's static method, that way if you don't override it the exception is caused.

Petah