Last active
August 13, 2018 23:31
-
-
Save pasindud/524b9cdc3566e7e52708 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
</programlisting> | |
</example> | |
</para> | |
+ | |
+ <sect2 xml:id="functions.scalar-type-declaration"> | |
+ <title>Type Declaration</title> | |
+ <note> | |
+ <para> | |
+ Type declaration is also known as Type Hinting. | |
+ </para> | |
+ </note> | |
+ <para> | |
+ 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, declare(strict_types=1) 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 Parameters examples</title> | |
+ <programlisting role="php"> | |
+<![CDATA[ | |
+<?php | |
+declare(strict_types=1); // Fatal error if this is not the first statement | |
+foo(); // strictly type-checked function call | |
+ | |
+function foobar() { | |
+ foo(); // strictly type-checked function call | |
+} | |
+ | |
+class baz { | |
+ function foobar() { | |
+ foo(); // strictly type-checked function call | |
+ } | |
+} | |
+ | |
+ | |
+/* | |
+ If this is file 2 and file 1 includes this file, | |
+ num() function calls from file 1 are not strictly type-checked unless | |
+ file 1 has set strict_types=1 | |
+*/ | |
+function num(int $a) { | |
+ return $a; | |
+} | |
+ | |
+]]> | |
+ </programlisting> | |
+ <programlisting role="php"> | |
+<![CDATA[ | |
+<?php // file 1 | |
+declare(strict_types=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> | |
+ </sect2> | |
+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
!Y@ at@Y!