rfc:static-classes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:static-classes [2008/05/03 15:13] – Fixing code example lstrojny | rfc:static-classes [2008/05/05 21:05] – __setStatic()/__getStatic() lstrojny | ||
---|---|---|---|
Line 4: | Line 4: | ||
* Author: Lars Strojny < | * Author: Lars Strojny < | ||
* Status: in the works | * Status: in the works | ||
+ | |||
===== Introduction ===== | ===== Introduction ===== | ||
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 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, | ||
+ | |||
+ | |||
===== Syntax ===== | ===== Syntax ===== | ||
Line 14: | Line 18: | ||
{ | { | ||
public static function staticMethod() | public static function staticMethod() | ||
- | {} | + | { |
+ | return ' | ||
+ | | ||
} | } | ||
abstract static class AbstractStaticClass | abstract static class AbstractStaticClass | ||
{ | { | ||
- | abstract | + | abstract public static abstractStaticMethod(); |
} | } | ||
+ | |||
+ | abstract static class AbstractStaticClass2 | ||
+ | { | ||
+ | public static function staticMethod() | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | StaticClass:: | ||
+ | AbstractStaticClass2:: | ||
</ | </ | ||
- | ===== Object | + | |
+ | |||
+ | ===== Class model rules ===== | ||
The following rules would apply for static classes: | The following rules would apply for static classes: | ||
Line 28: | Line 48: | ||
* Static methods in abstract static classes **may not** be called. They must be extended first | * Static methods in abstract static classes **may not** be called. They must be extended first | ||
* In static classes, abstract static methods **are allowed** again | * In static classes, abstract static methods **are allowed** again | ||
- | * Static classes **may not** have a constructor, | + | * Static classes **may not** have a constructor, |
* Static classes **may not** extend non-static classes | * Static classes **may not** extend non-static classes | ||
* The current behaviour of abstract classes/ | * The current behaviour of abstract classes/ | ||
+ | * '' | ||
===== Code ===== | ===== Code ===== | ||
- | * http:// | + | * A few features missing, but initial functionality works: [[http:// |
===== Further reading ===== | ===== Further reading ===== | ||
* [[http:// | * [[http:// |
rfc/static-classes.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1