svn: /phpdoc/en/trunk/appendices/migration70/ new-features.xml
| From: | Adam Harvey | Date: | Sun, 09 Aug 2015 01:17:08 +0000 |
| Subject: | svn: /phpdoc/en/trunk/appendices/migration70/ new-features.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-13551@lists.php.net to get a copy of this message | ||
aharvey Sun, 09 Aug 2015 01:17:08 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=337368
Log:
Cut down the return type docs in the migration guide.
Changed paths:
U phpdoc/en/trunk/appendices/migration70/new-features.xml
Modified: phpdoc/en/trunk/appendices/migration70/new-features.xml
===================================================================
--- phpdoc/en/trunk/appendices/migration70/new-features.xml 2015-08-09 01:17:04 UTC (rev 337367)
+++ phpdoc/en/trunk/appendices/migration70/new-features.xml 2015-08-09 01:17:08 UTC (rev 337368)
@@ -44,7 +44,8 @@
be placed at the top of the file. This means that the strictness of typing
for scalars is configured on a per-file basis. This directive not only affects
the type declarations of parameters, but also a function's return type (see
- Return Type Declarations), built-in PHP functions, and functions from loaded
+ <link linkend="functions.returning-values.type-declaration">return type
declarations</link>,
+ built-in PHP functions, and functions from loaded
extensions.
</para>
@@ -57,16 +58,18 @@
</sect2>
<sect2 xml:id="migration70.new-features.return-type-declarations">
- <title>Return Type Declarations</title>
+ <title>Return type declarations</title>
<para>
- Return type declarations enable for the return type of a function, method, or
- closure to be specified. The following return types are supported:
- <type>string</type>, <literal>int</literal>,
<type>float</type>,
- <literal>bool</literal>, <type>array</type>,
<type>callable</type>,
- <literal>self</literal> (methods only), <literal>parent</literal>
(methods
- only), <literal>Closure</literal>, the name of a class, and the name of an
- interface.
+ PHP 7 adds support for
+ <link linkend="functions.returning-values.type-declaration">return type
declarations</link>.
+ Similarly to
+ <link linkend="functions.arguments.type-declaration">argument type
declarations</link>,
+ return type declarations specify the type of the value that will be
+ returned from a function. The same
+ <link linkend="functions.arguments.type-declaration.types">types</link>
+ are available for return type declarations as are available for argument
+ type declarations.
</para>
<informalexample>
@@ -98,88 +101,11 @@
</informalexample>
<para>
- With respect to subtyping, invariance has been chosen for return types.
- This simply means that when a method is either overridden in a subtyped class
- or implemented as defined in a contract, its return type must match exactly
- the method it is (re)implementing.
+ Full documentation and examples of return type declarations can be found in
+ the
+ <link linkend="functions.returning-values.type-declaration">return type
declarations</link>.
+ reference.
</para>
-
- <informalexample>
- <programlisting role="php">
-<![CDATA[
-<?php
-
-class A {}
-class B extends A {}
-
-class C
-{
- public function test() : A
- {
- return new A;
- }
-}
-
-class D extends C
-{
- // overriding method C::test() : A
- public function test() : B // Fatal error due to variance mismatch
- {
- return new B;
- }
-}
-]]>
- </programlisting>
- &example.outputs;
- <screen>
-<![CDATA[
-PHP Fatal error: Declaration of D::test() must be compatible with C::test(): A
-]]>
- </screen>
- </informalexample>
-
- <para>
- The overriding method D::test() causes an E_COMPILE_ERROR because covariance
- is not allowed. In order for this to work, D::test() method must have a
- return type of class A.
- </para>
-
- <informalexample>
- <programlisting role="php">
-<![CDATA[
-<?php
-
-class A {}
-
-interface SomeInterface
-{
- public function test() : A;
-}
-
-class B implements SomeInterface
-{
- public function test() : A // all good!
- {
- return null; // incorrect return type
- }
-}
-
-(new B)->test();
-]]>
- </programlisting>
- &example.outputs;
- <screen>
-<![CDATA[
-Fatal error: Uncaught TypeError: Return value of B::test() must be an instance of A, null
returned...
-]]>
- </screen>
- </informalexample>
-
- <para>
- This time, the implemented method causes a <literal>TypeError</literal>
- exception to be thrown when executed. This is because <type>null</type> is
- not a valid return type - only an instance of the class A can be returned.
- </para>
</sect2>
<sect2 xml:id="migration70.new-features.null-coalesce-op">