Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created March 7, 2016 14:29
Show Gist options
  • Save chrisguitarguy/30ba1506b760af239b4d to your computer and use it in GitHub Desktop.
Save chrisguitarguy/30ba1506b760af239b4d to your computer and use it in GitHub Desktop.
TIL you cannot give PHP's scalar typehints any default value other than `null`. Completely with a misleading error message.
<?php
function foo(boolean $hello=true)
{
// ...
}
/*
local> php -l example.php
PHP Fatal error: Default value for parameters with a class type hint can only be NULL in t.php on line 3
Fatal error: Default value for parameters with a class type hint can only be NULL in t.php on line 3
Errors parsing example.php
*/
@jasonlotito
Copy link

Use bool, not boolean. Aliases are not supported. See the original RFC: https://wiki.php.net/rfc/scalar_type_hints_v5

@chrisguitarguy
Copy link
Author

Thanks @jasonlotito!

@nyamsprod
Copy link

https://3v4l.org/OiNiO <- still fails on HHVM but that's another issue 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment