Edit report at https://bugs.php.net/bug.php?id=47971&edit=1
ID: 47971 Comment by: phpmpan at mpan dot pl Reported by: cscott at ggot dot org Summary: Allow 'static' keyword to be applied to entire classes Status: Open Type: Feature/Change Request Package: Class/Object related Operating System: * PHP Version: * Block user comment: N Private report: N New Comment: One note: PHP allows calling static methods on class instances and this actually works faster* than pure static call. Compare: for (... many times ...) { UtilityClass::staticMethod(); } vs $instance = new UtilityClass(); for (... many times ...) { $instance->staticMethod(); } Creating an instance of a utility class is a neat optimization in heavy loops. Do not consider me an advocate of premature optimization, but if g*to was introduced in PHP because generated code may benefit from it, ability to create instances of utility classes should not be prohibited for the same reason. If OP's proposition is implemented, people will flock to it, effectively blocking ability to use the described solution. ____ * 25% less time consumed; tests were performed about a year ago Previous Comments: ------------------------------------------------------------------------ [2012-10-26 16:09:57] dagguh at gmail dot com What you are referring to is a utility class. It only has static members and a private constructor, which should never be called (even from the class itself). Your suggestion could be useful, because implementing private empty constructors is just boilerplate code. PS. Some people even throw an exception inside the private constructor. ------------------------------------------------------------------------ [2009-10-31 00:27:53] cscott at ggot dot org For Relevancy: I do not believe that namespaces solve this problem, as __autoload does not work with namespaces (and, for obvious reasons, shouldn't). ------------------------------------------------------------------------ [2009-04-14 21:07:14] cscott at ggot dot org Description: ------------ Fairly simple: A developer is allowed to define his/her classes as abstract or final, but not as static. For continuity's sake, it would be preferable to be able to declare classes as static as well. This would greatly ease the creation of static function collections/libraries, especially those included with __autoload(). When a class is declared as abstract, it is a statement at the open that this is an incomplete member; you can specify any method inside a class to be abstract and the class is effectively abstract, yet this keyword is allowed in the class declaration. When a class is declared final, it is a statement at the open that all members are to be considered final, and that this class should not be extended any further. By allowing classes to be declared as static, it would follow with allowing "abstract class foo" in the sense that the keyword reflects the contents of the class, and would follow with "final class foo" in that it would define a binding construct for all members of the class. Whether a) In a static class, all methods and members are automatically static -OR- b) In a static class, all methods and members must be declared static Is surely not for me to decide -- either is useful, as it either forces me to ensure all members are static, or it does the legwork for me. As such, I make no suggestion and defer to the wisdom of the developer(s). Thank you for your consideration. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=47971&edit=1