svn: /phpdoc/en/trunk/language/ functions.xml
| From: | Pasindu De Silva | Date: | Sat, 18 Jul 2015 12:06:20 +0000 |
| Subject: | svn: /phpdoc/en/trunk/language/ functions.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-13466@lists.php.net to get a copy of this message | ||
pasindu Sat, 18 Jul 2015 12:06:20 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=337187
Log:
Added initial return type declarations
Changed paths:
U phpdoc/en/trunk/language/functions.xml
Modified: phpdoc/en/trunk/language/functions.xml
===================================================================
--- phpdoc/en/trunk/language/functions.xml 2015-07-18 07:50:57 UTC (rev 337186)
+++ phpdoc/en/trunk/language/functions.xml 2015-07-18 12:06:20 UTC (rev 337187)
@@ -774,6 +774,8 @@
returned.
</para>
</note>
+ <sect2>
+ <title>Use of return</title>
<para>
<example>
<title>Use of <function>return</function></title>
@@ -837,6 +839,123 @@
For more information on references, please check out <link
linkend="language.references">References Explained</link>.
</simpara>
+ </sect2>
+ <sect2>
+ <title>Return Type Declarations</title>
+ <para>
+ PHP 7 Introduces Return type delaration where function can be forced to return
+ type that is defined. This done by setting
+ <link
linkend="control-structures.declare">declare(strict_types=1)</link>, if declare
strict_type directive
+ is set to 0 or not set functions are weakly type checked. The directive will have affect on
all functions
+ delcared in the file and not functions either included by this file or files that includes
this file, are
+ affected. The place where the function is called from is irrelavent.
+ Declarable types are Classes, Interfaces, Array, Int, Float, String and Bool.
+ </para>
+ <para>
+ <example>
+ <title>Affect of strict_types directive </title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+declare(strict_types=1); // file1.php
+
+// file 1 - strictly type checked
+// file 2 - weakly type checked
+
+include "file2.php";
+
+var_dump(a()); // works correctly
+var_dump(b()); // fatal TypeError
+
+
+function b() : int{
+ return "1";
+}
+?>
+
+
+<?php
+// file2.php
+function a(): int{
+ return "1";
+}
+
+?>
+]]>
+ </programlisting>
+ </example>
+ </para>
+ <para>
+ <example>
+ <title>More Return types</title>
+ <para>
+ The function signature must match of the parent, if the parent has not been declared the
+ return type the child can declare a type.
+ </para>
+ <programlisting role="php">
+<![CDATA[
+<?php
+declare(strict_types=1);
+
+interface Numbers{
+ abstract public function one() : int ;
+ abstract public function two(); // no return type defined
+}
+
+class NumClass implements Numbers
+{
+ public function one() : int { // return type same as abstract
+ return 0;
+ }
+
+ public function two() { // return type defined due lack of return type
+ return 2; // in parent abstract function
+ }
+}
+
+class display extends NumClass
+{
+ public function one() : int // return type same as parent
+ {
+ return 1;
+ }
+
+ public function two() : int // return type same as parent
+ {
+ return 2;
+ }
+}
+
+$dis = new display();
+var_dump($dis->one());
+var_dump($dis->two());
+
+?>
+]]>
+ </programlisting>
+ </example>
+ </para>
+ <para>
+ <example>
+ <title>Return a class;</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+declare(strict_types=1);
+
+interface A {
+ static function make();
+}
+class B implements A {
+ static function make(): B { //valid
+ return new B();
+ }
+}
+]]>
+ </programlisting>
+ </example>
+ </para>
+ </sect2>
</sect1>
<sect1 xml:id="functions.variable-functions">
@@ -1259,4 +1378,4 @@
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
--->
+-->
\ No newline at end of file