Junior — Middle
Is it possible to implement a static method inside an interface definition?
sobes.tech AI
Answer from AI
In PHP, interfaces could not contain static methods before PHP 8.0. Starting with PHP 8.0, it became possible to declare static methods in interfaces.
This allows defining a contract that requires implementing static methods in classes that implement the interface.
Example (PHP 8.0+):
interface MyInterface {
public static function staticMethod();
}
class MyClass implements MyInterface {
public static function staticMethod() {
echo "Static method implemented";
}
}
MyClass::staticMethod();
Before PHP 8.0, such a thing was impossible, and static methods were not declared in interfaces.