Static classes are a well known construct for utility classes or stateless abstraction. C# for example has an class System.Environment to access command line options, the current directory, the name of the machine where the program is running and so on. In PHP the current practice is to use an abstract class with static methods. Static classes can act as utility containers. For such utility containers or for more complex static inheritance, static classes are a helpful tool. This is why they are proposed for PHP.
<?php static class StaticClass { public static function staticMethod() { return 'foo'; } } abstract static class AbstractStaticClass { abstract public static abstractStaticMethod(); } abstract static class AbstractStaticClass2 { public static function staticMethod() { return 'bar'; } } StaticClass::staticMethod(); // (string)'foo' AbstractStaticClass2::staticMethod(); // Throws error, not allowed. Must be extended first
The following rules would apply for static classes:
__toString()
__setStatic()
and __getStatic()
will provide functionality similiar to __get()
and __set()