php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #66814 Make typing hinting arrays use the new shorthand syntax
Submitted: 2014-03-03 10:04 UTC Modified: 2014-03-03 10:49 UTC
From: tom at r dot je Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.5.9 OS: Any
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tom at r dot je
New email:
PHP Version: OS:

 

 [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.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-03-03 10:22 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2014-03-03 10:22 UTC] nikic@php.net
"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 ;)
 [2014-03-03 10:49 UTC] tom at r dot je
I can see the reasoning but it's still inconsistent. In java we still have the [] notation for the type. For example:

int[] myArray = new int[4];

or

public static void main(String[] args)


Where the array hinting and type syntax is the same as the creation syntax without the new keyword.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 09:00:02 2026 UTC