svn: /phpdoc/en/trunk/language/ functions.xml

From: Date: Sat, 04 Jul 2015 23:18:07 +0000
Subject: svn: /phpdoc/en/trunk/language/ functions.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-13438@lists.php.net to get a copy of this message
pasindu Sat, 04 Jul 2015 23:18:07 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=337104 Log: Added intial Type Declarations docs PHP 7 Added Functions changelog Changed paths: U phpdoc/en/trunk/language/functions.xml Modified: phpdoc/en/trunk/language/functions.xml =================================================================== --- phpdoc/en/trunk/language/functions.xml 2015-07-03 19:59:20 UTC (rev 337103) +++ phpdoc/en/trunk/language/functions.xml 2015-07-04 23:18:07 UTC (rev 337104) @@ -176,8 +176,37 @@ Information may be passed to functions via the argument list, which is a comma-delimited list of expressions. The arguments are evaluated from left to right. - </simpara> + </simpara> <para> + <informaltable> + <tgroup cols="2"> + <thead> + <row> + <entry>&Version;</entry> + <entry>&Description;</entry> + </row> + </thead> + <tbody> + <row> + <entry>5.1.0</entry> + <entry>classes,interfaces and arrays type declaration</entry> + </row> + <row> + <entry>5.4.0</entry> + <entry><type>callable</type> type declaration</entry> + </row> + <row> + <entry>7.0.0</entry> + <entry>scalar (<type>int</type>, + <type>float</type>, + <type>string</type>, + <type>bool</type>) type declaration</entry> + </row> + </tbody> + </tgroup> + </informaltable> + </para> + <para> PHP supports passing arguments by value (the default), <link linkend="functions.arguments.by-reference">passing by reference</link>, and <link @@ -199,8 +228,7 @@ ]]> </programlisting> </example> - </para> - + </para> <sect2 xml:id="functions.arguments.by-reference"> <title>Making arguments be passed by reference</title> @@ -232,9 +260,7 @@ </programlisting> </example> </para> - </sect2> - <sect2 xml:id="functions.arguments.default"> <title>Default argument values</title> @@ -356,9 +382,143 @@ As of PHP 5, arguments that are passed by reference may have a default value. </simpara> </note> + </sect2> + <sect2 xml:id="functions.scalar-type-declaration"> + <title>Scalar Type Declaration</title> + <para> + <note> + <para> + Type declaration is also known as Type Hinting. + </para> + </note> + PHP 7 introduces scalar type declaration. Functions are now able to force parameters + to be <type>string</type>, <type>int</type>, <type>float</type> or <type>bool</type>. + By default all PHP Files are in weakly typed mode. + To enable strict type checking, <link linkend="control-structures.declare">declare(strict_types=1)</link> + directive must be the first + statement in the file. strict_types has two options, 1 for strict type + checking and 0 for weak type checking. This only affects the file this is stated in and not + files either included in this file or other files that include this file. + Whether or not the function being called was declared in a file that uses strict or weak + type checking is irrelevant. The type checking mode depends on the file where the function is called from. + </para> + <para> + <example> + <title>Scalar Type Declaration examples</title> + <programlisting role="php"> +<![CDATA[ +<?php +declare(strict_types=1); // Fatal error if this is not the first statement +include "foo.php"; +foo(); // strictly type-checked function call + +function foobar() { + foo(); // strictly type-checked function call +} + +class baz { + function foobar() { + foo(); // strictly type-checked function call + } +} + +]]> + </programlisting> + <programlisting role="php"> +<![CDATA[ +<?php +declare(strict_types=1); // file 1 + +/* + file 1 - strictly type checked + file 2 - weakly type checked +*/ + +include 'file2.php'; + +a(1); // strictly type checked +b(1); // strictly type checked + +function a(int $a) { + return $a; +} + +?> + +<?php // file 2 + +b(1); // weakly type checked + +function b(int $a) { + return $a; +} + +?> + +]]> + </programlisting> + <programlisting role="php"> +<![CDATA[ +<?php // file 1 + +/* + file 1 - weakly type checked + file 2 - strictly type checked +*/ + +include 'file2.php'; + +a(1); // weakly type checked +b(1); // weakly type checked + +function a(int $a) { + return $a; +} + + +?> + +<?php // file 2 +declare(strict_types=1); + +b(1); // strictly type checked + +function b(int $a) { + return $a; +} + +?> + +]]> + </programlisting> + </example> + </para> + + + <sect3> + <para> + <example> + <title>Type Widening</title> + <programlisting role="php"> +<![CDATA[ +<?php +declare(strict_types=1); + +function a(float $a) { + var_dump($a); // float(1) +} + +$a = 1; // int(1) +var_dump($a); +a($a); + +]]> + </programlisting> + </example> + </para> + </sect3> </sect2> - <sect2 xml:id="functions.variable-arg-list"> <title>Variable-length argument lists</title>

« previous php.doc.cvs (#13438) next »