Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107806 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 24080 invoked from network); 17 Nov 2019 16:48:57 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 17 Nov 2019 16:48:57 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id D51872D202E for ; Sun, 17 Nov 2019 06:41:22 -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 ; Sun, 17 Nov 2019 06:41:22 -0800 (PST) Received: from [192.168.178.20] (x4d008c57.dyn.telefonica.de [77.0.140.87]) by dd46610.kasserver.com (Postfix) with ESMTPSA id 3DC0043446C5 for ; Sun, 17 Nov 2019 15:41:21 +0100 (CET) To: internals@lists.php.net Organization: Aimeos GmbH Message-ID: <461958fe-182e-59bf-9fdb-b110fd09e2b2@aimeos.com> Date: Sun, 17 Nov 2019 15:41:20 +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: Concept: "arrayable" pseudo type and \Arrayable interface From: norbert@aimeos.com (Aimeos | Norbert Sendetzky) Since PHP 7.1 there's the "iterable" pseudo type hint that matches "array" and "Traversable". PHP frameworks would profit from support of an "arrayable" pseudo type hint that matches "array" and all objects that implements "Traversable", "ArrayAccess" and "Countable". Thus, we could pass arrays or all objects that behave like arrays to methods and do: function useArrayable( arrayable $arg ) : arrayable { $cnt = count( $arg ); $value = $arg['key']; foreach( $arg as $key => $entry ) { ... } return $arg; } It would be useful to implement an Arrayable interface too: interface Arrayable extends \Iterator, \Countable, \ArrayAccess { public function toArray() : array; } Then, we can use array like objects: class Test implements \Arrayable { // ... } $arrayable = new Test(); $arrayable['key'] = $value; $arrayable = useArrayable( $arrayable ); And if necessary, we can convert them to native arrays: if( $arrayable instanceof \Arrayable ) { $arrayable = $arrayable->toArray(); }