From 5486657cd95d764253b335c26b201c3693772704 Mon Sep 17 00:00:00 2001 From: Pasindu De Silva <pasindu@php.net> Date: Sat, 4 Jul 2015 23:18:07 +0000 Subject: [PATCH] Added intial Type Declarations docs PHP 7 Added Functions changelog git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@337104 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/functions.xml | 172 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 166 insertions(+), 6 deletions(-) diff --git a/language/functions.xml b/language/functions.xml index 4277add82b..8ca45c8808 100644 --- a/language/functions.xml +++ b/language/functions.xml @@ -176,7 +176,36 @@ function recursion($a) 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 @@ -199,8 +228,7 @@ function takes_array($input) ]]> </programlisting> </example> - </para> - + </para> <sect2 xml:id="functions.arguments.by-reference"> <title>Making arguments be passed by reference</title> @@ -232,9 +260,7 @@ echo $str; // outputs 'This is a string, and something extra.' </programlisting> </example> </para> - </sect2> - <sect2 xml:id="functions.arguments.default"> <title>Default argument values</title> @@ -356,9 +382,143 @@ Making a bowl of acidophilus raspberry. 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>