Skip to content

Instantly share code, notes, and snippets.

@deshack
Last active May 27, 2016 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deshack/fa50aacfa1376fa7623e89481d689003 to your computer and use it in GitHub Desktop.
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
<?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