language/oop5/interfaces.xml
3d522c890d98c563bb283cf89ec5da5f535cfb8f
...
...
@@ -58,12 +58,6 @@
58
58
</para>
59
59
<warning>
60
60
<para>
61
-
A class can implement two interfaces which define a method with the
62
-
same name, only if the method declaration in both interfaces is identical.
63
-
</para>
64
-
</warning>
65
-
<warning>
66
-
<para>
67
61
A class that implements an interface may use a different name for its parameters than
68
62
the interface. However, as of PHP 8.0 the language supports <link linkend="functions.named-arguments">named arguments</link>, which means
69
63
callers may rely on the parameter name in the interface. For that reason, it is strongly
...
...
@@ -79,17 +73,20 @@
79
73
<note>
80
74
<para>
81
75
The class implementing the interface must declare all methods in the interface
82
-
with a <link linkend="language.oop.lsp">compatible signature</link>.
76
+
with a <link linkend="language.oop.lsp">compatible signature</link>. A class can implement multiple interfaces
77
+
which declare a method with the same name. In this case, the implementation must follow the
78
+
<link linkend="language.oop.lsp">signature compatibility rules</link> for all the interfaces. So
79
+
<link linkend="language.oop5.variance">covariance and contravariance</link> can be applied.
83
80
</para>
84
81
</note>
85
82
</sect2>
86
83
<!-- Move this to OOP constants page? -->
87
84
<sect2 xml:id="language.oop5.interfaces.constants">
88
-
<title><literal>Constants</literal></title>
85
+
<title>Constants</title>
89
86
<para>
90
87
It's possible for interfaces to have constants. Interface constants work exactly
91
-
like <link linkend="language.oop5.constants">class constants</link> except
92
-
they cannot be overridden by a class/interface that inherits them.
88
+
like <link linkend="language.oop5.constants">class constants</link>.
89
+
Prior to PHP 8.1.0, they cannot be overridden by a class/interface that inherits them.
93
90
</para>
94
91
</sect2>
95
92
<sect2 xml:id="language.oop5.interfaces.examples">
...
...
@@ -186,6 +183,33 @@ class D implements B
186
183
]]>
187
184
</programlisting>
188
185
</example>
186
+
<example xml:id="language.oop5.interfaces.examples.variance.multiple.interfaces">
187
+
<title>Variance compatibility with multiple interfaces</title>
188
+
<programlisting role="php">
189
+
<![CDATA[
190
+
<?php
191
+
class Foo {}
192
+
class Bar extends Foo {}
193
+

194
+
interface A {
195
+
public function myfunc(Foo $arg): Foo;
196
+
}
197
+

198
+
interface B {
199
+
public function myfunc(Bar $arg): Bar;
200
+
}
201
+

202
+
class MyClass implements A, B
203
+
{
204
+
public function myfunc(Foo $arg): Bar
205
+
{
206
+
return new Bar();
207
+
}
208
+
}
209
+
?>
210
+
]]>
211
+
</programlisting>
212
+
</example>
189
213
<example xml:id="language.oop5.interfaces.examples.ex3">
190
214
<title>Multiple interface inheritance</title>
191
215
<programlisting role="php">
...
...
@@ -238,12 +262,15 @@ interface A
238
262
echo A::B;
239
263

240
264

241
-
// This will however not work because it's not allowed to
242
-
// override constants.
243
265
class B implements A
244
266
{
245
267
const B = 'Class constant';
246
268
}
269
+

270
+
// Prints: Class constant
271
+
// Prior to PHP 8.1.0, this will however not work because it was not
272
+
// allowed to override constants.
273
+
echo B::B;
247
274
?>
248
275
]]>
249
276
</programlisting>
...
...
@@ -264,7 +291,7 @@ interface A
264
291
// Classes that extend the abstract class must implement the rest.
265
292
abstract class B implements A
266
293
{
267
-
pubic function foo(string $s): string
294
+
public function foo(string $s): string
268
295
{
269
296
return $s . PHP_EOL;
270
297
}
271
298