rfc:static-classes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rfc:static-classes [2008/05/03 15:21]
lstrojny shorter link
rfc:static-classes [2017/09/22 13:28] (current)
Line 9: Line 9:
 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, static classes are a helpful tool. This is why they are proposed for PHP. 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.
 +
 +
  
 ===== Syntax ===== ===== Syntax =====
Line 16: Line 18:
 { {
     public static function staticMethod()     public static function staticMethod()
-    {}+    { 
 +        return 'foo'; 
 +    }
 } }
  
 abstract static class AbstractStaticClass abstract static class AbstractStaticClass
 { {
-    abstract static public static abstractStaticMethod();+    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
 </code> </code>
-===== Object model rules =====+ 
 + 
 + 
 + 
 +===== Class model rules =====
 The following rules would apply for static classes: The following rules would apply for static classes:
  
Line 30: Line 50:
   * 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, destructor, dynamic interceptors or ''__toString()''+  * Static classes **may not** have a constructor, destructor, dynamic interceptors or ''%%__toString()%%''
   * Static classes **may not** extend non-static classes   * Static classes **may not** extend non-static classes
   * The current behaviour of abstract classes/non-static classes with static members **would not** change. This is important for backwards compatibility   * The current behaviour of abstract classes/non-static classes with static members **would not** change. This is important for backwards compatibility
- +  * ''%%__setStatic()%%'' and ''%%__getStatic()%%'' **will** provide functionality similiar to ''%%__get()%%'' and ''%%__set()%%'' 
 +  * Static classes **cannot** be instantiated 
 +  * Static classes **can** implement interfaces containing only static methods
  
 ===== Code ===== ===== Code =====
-  * A few features missing, but initial functionality works: [[http://lars.schokokeks.org/php/static-classes-001.diff|static-classes-001.diff]]+  * A few features missing, but initial functionality works: [[http://lars.schokokeks.org/php/static-classes-002.diff|static-classes-002.diff]]
  
 ===== Further reading ===== ===== Further reading =====
   * [[http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx|Static classes in C#]]   * [[http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx|Static classes in C#]]
rfc/static-classes.1209828097.txt.gz · Last modified: 2017/09/22 13:28 (external edit)