|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-03-03 10:04 UTC] tom at r dot je
Description:
------------
Since the shorthand array syntax was introduced in PHP5.4, array type hints have been a little inconsistent. It would be preferable and more consistent if this worked:
function expectsArray([] $arr) {
}
expectsArray(['a', 'b']);
As it stands, you can set the default value using [], this is valid code:
function expectsArray(array $arr = []) {
}
expectsArray(['a', 'b']);
It would make more sense to also type-hint an array using the shorthand notation.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 07 20:00:02 2026 UTC |
"array" here is a type name. It is not directly related to the syntax for constructing an array. After all, you do the typehints like this: function test(array $arr); function test(SomeClass $obj); function test(callable $callback); And not like this: function test(array() $arr); function test(new SomeClass $obj); function test(function() {} $callback); "array" is a type, while "array()", "array('a', 'b')", "[]" and "['a', 'b']" are just specific instances of that type. That's why [] is not allowed in that context ;)