language/oop5/static.xml
3c831023489be0c64268882d5b656149140d6f8b
...
...
@@ -8,7 +8,8 @@
8
8
This page describes the use of the <literal>static</literal> keyword to
9
9
define static methods and properties. <literal>static</literal> can also
10
10
be used to
11
-
<link linkend="language.variables.scope.static">define static variables</link>
11
+
<link linkend="language.variables.scope.static">define static variables</link>,
12
+
<link linkend="functions.anonymous-functions.static">define static anonymous functions</link>
12
13
and for
13
14
<link linkend="language.oop5.late-static-bindings">late static bindings</link>.
14
15
Please refer to those pages for information on those meanings of
...
...
@@ -18,16 +19,8 @@
18
19

19
20
<para>
20
21
Declaring class properties or methods as static makes them accessible
21
-
without needing an instantiation of the class. A property declared as
22
-
static cannot be accessed with an instantiated class object (though
23
-
a static method can).
24
-
</para>
25
-

26
-
<para>
27
-
For compatibility with PHP 4, if no <link
28
-
linkend="language.oop5.visibility">visibility</link>
29
-
declaration is used, then the property or method will be treated
30
-
as if it was declared as <literal>public</literal>.
22
+
without needing an instantiation of the class.
23
+
These can also be accessed statically within an instantiated class object.
31
24
</para>
32
25

33
26
<sect2 xml:id="language.oop5.static.methods">
...
...
@@ -36,22 +29,17 @@
36
29
<para>
37
30
Because static methods are callable without an instance of
38
31
the object created, the pseudo-variable <varname>$this</varname> is
39
-
not available inside the method declared as static.
32
+
not available inside methods declared as static.
40
33
</para>
41
34

42
-
<caution>
43
-
<simpara>
44
-
In PHP 5, calling non-static methods statically generates an
45
-
<constant>E_STRICT</constant> level warning.
46
-
</simpara>
47
-
</caution>
48
-

49
35
<warning>
50
-
<simpara>
51
-
In PHP 7, calling non-static methods statically is deprecated, and will
52
-
generate an <constant>E_DEPRECATED</constant> warning. Support for
53
-
calling non-static methods statically may be removed in the future.
54
-
</simpara>
36
+
<para>
37
+
Calling non-static methods statically throws an <classname>Error</classname>.
38
+
</para>
39
+
<para>
40
+
Prior to PHP 8.0.0, calling non-static methods statically were deprecated, and
41
+
generated an <constant>E_DEPRECATED</constant> warning.
42
+
</para>
55
43
</warning>
56
44

57
45
<example>
...
...
@@ -67,7 +55,7 @@ class Foo {
67
55

68
56
Foo::aStaticMethod();
69
57
$classname = 'Foo';
70
-
$classname::aStaticMethod(); // As of PHP 5.3.0
58
+
$classname::aStaticMethod();
71
59
?>
72
60
]]>
73
61
</programlisting>
...
...
@@ -78,20 +66,14 @@ $classname::aStaticMethod(); // As of PHP 5.3.0
78
66
<title>Static properties</title>
79
67

80
68
<para>
81
-
Static properties cannot be accessed through the object using the arrow
82
-
operator -&gt;.
83
-
</para>
84
-

85
-
<para>
86
-
Like any other PHP static variable, static properties may only be
87
-
initialized using a literal or constant before PHP 5.6; expressions are
88
-
not allowed. In PHP 5.6 and later, the same rules apply as &const;
89
-
expressions: some limited expressions are possible, provided they can be
90
-
evaluated at compile time.
69
+
Static properties are accessed using the
70
+
<link linkend="language.oop5.paamayim-nekudotayim">Scope Resolution Operator</link>
71
+
(<literal>::</literal>) and cannot be accessed through the object operator
72
+
(<literal>-&gt;</literal>).
91
73
</para>
92
74

93
75
<para>
94
-
As of PHP 5.3.0, it's possible to reference the class using a variable.
76
+
It's possible to reference the class using a variable.
95
77
The variable's value cannot be a keyword (e.g. <literal>self</literal>,
96
78
<literal>parent</literal> and <literal>static</literal>).
97
79
</para>
...
...
@@ -126,7 +108,7 @@ print $foo->my_static . "\n"; // Undefined "Property" my_static
126
108

127
109
print $foo::$my_static . "\n";
128
110
$classname = 'Foo';
129
-
print $classname::$my_static . "\n"; // As of PHP 5.3.0
111
+
print $classname::$my_static . "\n";
130
112

131
113
print Bar::$my_static . "\n";
132
114
$bar = new Bar();
...
...
@@ -134,10 +116,25 @@ print $bar->fooStatic() . "\n";
134
116
?>
135
117
]]>
136
118
</programlisting>
119
+
&example.outputs.8.similar;
120
+
<screen>
121
+
<![CDATA[
122
+
foo
123
+
foo
124
+

125
+
Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23
126
+

127
+
Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23
128
+

129
+
foo
130
+
foo
131
+
foo
132
+
foo
133
+
]]>
134
+
</screen>
137
135
</example>
138
136
</sect2>
139
137
</sect1>
140
-
141
138
<!-- Keep this comment at the end of the file
142
139
Local variables:
143
140
mode: sgml
144
141