Req #66814 [Wfx]: Make typing hinting arrays use the new shorthand syntax

From: Date: Mon, 03 Mar 2014 10:49:13 +0000
Subject: Req #66814 [Wfx]: Make typing hinting arrays use the new shorthand syntax
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-184504@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66814&edit=1 ID: 66814 User updated by: tom at r dot je Reported by: tom at r dot je Summary: Make typing hinting arrays use the new shorthand syntax Status: Wont fix Type: Feature/Change Request Package: *General Issues Operating System: Any PHP Version: 5.5.9 Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2014-03-03 10:22:59] 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:04:33] 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. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=66814&edit=1

« previous php.bugs (#184504) next »