diff --git a/language/oop5/constants.xml b/language/oop5/constants.xml index 1828a61d2d..af033f51c4 100644 --- a/language/oop5/constants.xml +++ b/language/oop5/constants.xml @@ -1,20 +1,21 @@ - + Class Constants It is possible to define constant values on a per-class basis remaining the same and unchangeable. Constants differ from normal variables in that you - don't use the $ symbol to declare or use them. Like - static members, constant values - cannot be accessed from an instance of the object (using - $object::constant). + don't use the $ symbol to declare or use them. The value must be a constant expression, not (for example) a variable, a class member, result of a mathematical operation or a function call. + + As of PHP 5.3.0, it's possible to reference the class using a variable. + + Defining and using a constant @@ -31,9 +32,13 @@ class MyClass echo MyClass::constant . "\n"; +$classname = "MyClass"; +echo $classname::constant . "\n"; + $class = new MyClass(); $class->showConstant(); -// echo $class::constant; is not allowed + +echo $class::constant."\n"; ?> ]]> diff --git a/language/oop5/paamayim-nekudotayim.xml b/language/oop5/paamayim-nekudotayim.xml index 422b557e0d..9c7f711d93 100644 --- a/language/oop5/paamayim-nekudotayim.xml +++ b/language/oop5/paamayim-nekudotayim.xml @@ -1,5 +1,5 @@ - + Scope Resolution Operator (::) @@ -16,6 +16,10 @@ the name of the class. + + As of PHP 5.3.0, it's possible to reference the class using a variable. + + Paamayim Nekudotayim would, at first, seem like a strange choice for naming a double-colon. However, while writing the Zend Engine 0.5 @@ -32,6 +36,9 @@ class MyClass { const CONST_VALUE = 'A constant value'; } +$classname = 'MyClass'; +echo $classname::CONST_VALUE; + echo MyClass::CONST_VALUE; ?> ]]> @@ -58,6 +65,9 @@ class OtherClass extends MyClass } } +$classname = 'OtherClass'; +echo $classname::doubleColon(); + OtherClass::doubleColon(); ?> ]]> diff --git a/language/oop5/static.xml b/language/oop5/static.xml index d0a47ead47..8400906cab 100644 --- a/language/oop5/static.xml +++ b/language/oop5/static.xml @@ -1,5 +1,5 @@ - + Static Keyword @@ -32,6 +32,10 @@ Calling non-static methods statically generates an E_STRICT level warning. + + As of PHP 5.3.0, it's possible to reference the class using a variable. + + Static member example @@ -60,7 +64,9 @@ $foo = new Foo(); print $foo->staticValue() . "\n"; print $foo->my_static . "\n"; // Undefined "Property" my_static -// $foo::my_static is not possible +print $foo::$my_static . "\n"; +$classname = 'Foo'; +print $classname::$my_static . "\n"; print Bar::$my_static . "\n"; $bar = new Bar(); @@ -82,6 +88,8 @@ class Foo { } Foo::aStaticMethod(); +$classname = 'Foo'; +$classname::aStaticMethod(); ?> ]]>