Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107840 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 42841 invoked from network); 20 Nov 2019 00:28:29 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 20 Nov 2019 00:28:29 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 1A1642D2097 for ; Tue, 19 Nov 2019 14:21:29 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS34788 85.13.163.0/24 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) Received: from dd46610.kasserver.com (dd46610.kasserver.com [85.13.163.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Tue, 19 Nov 2019 14:21:28 -0800 (PST) Received: from [192.168.178.20] (x4d01e228.dyn.telefonica.de [77.1.226.40]) by dd46610.kasserver.com (Postfix) with ESMTPSA id B40334340530 for ; Tue, 19 Nov 2019 23:21:26 +0100 (CET) To: internals@lists.php.net Organization: Aimeos GmbH Message-ID: <861afae0-4568-745f-6615-a252067cc506@aimeos.com> Date: Tue, 19 Nov 2019 23:21:26 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Envelope-From: Subject: [RFC] "arrayable" pseudo type hint From: norbert@aimeos.com (Aimeos | Norbert Sendetzky) Since PHP 7.1, there's the "iterable" pseudo type hint that matches "array" or "Traversable". PHP frameworks would profit from support of an "arrayable" pseudo type hint that matches "array" and all objects that can be used like arrays. For that, "arrayable" objects have to implement these interfaces: - ArrayAccess - Countable - Traversable (i.e. either Iterator or IteratorAggregate) Implementing "arrayable" pseudo type, we could pass arrays or all objects that can be used like arrays to methods and do: function useArrayable( arrayable $arg ) : arrayable { $cnt = count( $arg ); $value = $arg['key']; foreach( $arg as $key => $entry ); return $arg; } Best use cases are: - Laravel Collection (https://github.com/laravel/framework/blob/6.x/src/Illuminate/Support/Collection.php) - Aimeos Map (https://github.com/aimeos/map) No new interface is proposed because we can check if objects implement: ArrayAccess && Countable && Traversable Because "array" !== "arrayable", "arrayable objects will not be accepted by array_* functions and all functions that only use "array" as type hint for parameters and return types. This proposal is not obsolete by the implementation of union types (i.e. array|Traversable) because union data types are types/interfaces combined by OR. "arrayable" is a combination of OR and AND: array|ArrayAccess&Countable&Traversable Also, "arrayable" won't supersede "iterable" because - "iterable" matches arrays, iterators and generators - "arrayable" matches arrays and objects that can be used like arrays "Can be used like arrays" doesn't include support for empty() because it works for empty arrays but not for "arrayable" objects without elements. If possible and requested, this may be addressed by another RFC. As Larry Garfield suggested, this pre-RFC proposes concentrates on the "arrayable" pseudo type only. It does NOT discusses that: - arrayable objects can be converted to arrays (Steven Wade works on an RFC for a __toArray() magic function) - ArrayObject or any other arrayable object makes the array_* functions less relevant in the future - arrayable objects can be passed to array_* functions in the future