Last active
May 27, 2016 14:40
-
-
Save deshack/fa50aacfa1376fa7623e89481d689003 to your computer and use it in GitHub Desktop.
PHP7 Type Hinting with Casting - see https://gist.github.com/deshack/e63358a8726b7c3e0ba13563e4f9864c for a full collection of examples about PHP5/PHP7 type hinting
This file contains hidden or 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
<?php | |
function setBool(bool $bool) { | |
var_dump($bool); | |
} | |
setBool(true); // bool(true) | |
setBool('foo'); // bool(true) | |
setBool(''); // bool(false) | |
setBool(1); // bool(true) | |
setBool(-1); // bool(true) | |
setBool(0); // bool(false) | |
setBool([]); | |
// PHP Warning: Uncaught TypeError: Argument 1 passed to setBool() must be of the type boolean, array given | |
function setInt(int $number) { | |
var_dump($number); | |
} | |
setInt(10); // int(10) | |
setInt('10'); // int(10) | |
setInt('foo'); | |
// PHP Warning: Uncaught TypeError: Argument 1 passed to setInt() must be of the type integer, string given |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment