Document dynamic access of static methods/constants/members

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@244547 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Etienne Kneuss 2007-10-21 15:57:09 +00:00
parent 74064073b8
commit 6455a018a7
3 changed files with 32 additions and 9 deletions

View file

@ -1,20 +1,21 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.10 $ -->
<!-- $Revision: 1.11 $ -->
<sect1 xml:id="language.oop5.constants" xmlns="http://docbook.org/ns/docbook">
<title>Class Constants</title>
<para>
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 <varname>$</varname> symbol to declare or use them. Like
<link linkend="language.oop5.static">static</link> members, constant values
cannot be accessed from an instance of the object (using
<literal>$object::constant</literal>).
don't use the <varname>$</varname> symbol to declare or use them.
</para>
<para>
The value must be a constant expression, not (for example) a variable, a
class member, result of a mathematical operation or a function call.
</para>
<para>
As of PHP 5.3.0, it's possible to reference the class using a variable.
</para>
<example>
<title>Defining and using a constant</title>
<programlisting role="php">
@ -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";
?>
]]>
</programlisting>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.10 $ -->
<!-- $Revision: 1.11 $ -->
<sect1 xml:id="language.oop5.paamayim-nekudotayim" xmlns="http://docbook.org/ns/docbook">
<title>Scope Resolution Operator (::)</title>
@ -16,6 +16,10 @@
the name of the class.
</para>
<para>
As of PHP 5.3.0, it's possible to reference the class using a variable.
</para>
<para>
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();
?>
]]>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.13 $ -->
<!-- $Revision: 1.14 $ -->
<sect1 xml:id="language.oop5.static" xmlns="http://docbook.org/ns/docbook">
<title>Static Keyword</title>
@ -32,6 +32,10 @@
Calling non-static methods statically generates an E_STRICT level warning.
</para>
<para>
As of PHP 5.3.0, it's possible to reference the class using a variable.
</para>
<example>
<title>Static member example</title>
<programlisting role="php">
@ -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();
?>
]]>
</programlisting>