Skip to content

Instantly share code, notes, and snippets.

@deshack
Created May 27, 2016 14:10
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/e63358a8726b7c3e0ba13563e4f9864c to your computer and use it in GitHub Desktop.
Save deshack/e63358a8726b7c3e0ba13563e4f9864c to your computer and use it in GitHub Desktop.
PHP7 Scalar Type Hinting
<?php
/**
* PHP5 Type Hinting Example.
*
* @copyright 2016 SqueezyWeb
* @author Mattia Migliorini <mattia@squeezyweb.com>
*/
/**
* Array Type Hinting.
*/
function setOptions(array $options) {
var_dump($options);
}
// Call setOptions() passing an array.
setOptions(array());
// Output:
// array(0) {
// }
// Call setOptions() passing something else (a string in this case).
setOptions('foo');
// Result:
// PHP Warning: Uncaught TypeError: Argument 1 passed to setOptions() must be of the type array, string given
/**
* String Type Hinting.
*/
function setValue(string $value) {
var_dump($value);
}
setValue('foo');
// Result:
// PHP Catchable fatal error: Argument 1 passed to setValue() must be an instance of string, string given
<?php
/**
* PHP7 Type Hinting with Casting Example.
*
* @copyright 2016 SqueezyWeb
* @author Mattia Migliorini <mattia@squeezyweb.com>
*/
/**
* Boolean Type Hinting.
*/
function setBool(bool $bool) {
var_dump($bool);
}
// Call setBool() passing a boolean.
setBool(true);
// Output:
// bool(true)
// Call setBool() passing a string.
setBool('foo');
// Output:
// bool(true)
// Call setBool() passing an empty string.
setBool('');
// Output:
// bool(false)
// Call setBool() passing an integer.
setBool(1);
// Output:
// bool(true)
// Call setBool() passing a negative integer.
setBool(-1);
// Output:
// bool(true)
// Call setBool() passing zero.
setBool(0);
// Output:
// bool(false)
// Call setBool() passing an array.
setBool([]);
// Result:
// PHP Warning: Uncaught TypeError: Argument 1 passed to setBool() must be of the type boolean, array given
/**
* Integer Type Hinting.
*/
function setInt(int $number) {
var_dump($number);
}
// Call setInt() passing an integer.
setInt(10);
// Output:
// int(10)
// Call setInt() passing a numeric string.
setInt('10');
// Output:
// int(10)
// Call setInt() passing a non-numeric string.
setInt('foo');
// Result:
// PHP Warning: Uncaught TypeError: Argument 1 passed to setInt() must be of the type integer, string given
<?php
/**
* PHP7 Strict Type Hinting Example.
*
* @copyright 2016 SqueezyWeb
* @author Mattia Migliorini <mattia@squeezyweb.com>
*/
// Force Type Hinting to do a strict type check (the same as ===).
declare(strict_types=1);
function setBool(bool $bool) {
var_dump($bool);
}
// Call setBool() passing a string.
setBool('foo');
// Result:
// PHP Fatal error: Uncaught TypeError: Argument 1 passed to setBool() must be of the type boolean, string given
<?php
/**
* PHP7 Type Hinting Example.
*
* @copyright 2016 SqueezyWeb
* @author Mattia Migliorini <mattia@squeezyweb.com>
*/
/**
* String Type Hinting.
*/
function setValue(string $value) {
var_dump($value);
}
// Call setValue() passing a string.
setValue('foo');
// Output:
// string(3) "foo"
// Call setValue() passing an array.
setValue([]);
// Result:
// PHP Warning: Uncaught TypeError: Argument 1 passed to setValue() must be of the type string, array given
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment