Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107807 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 42968 invoked from network); 17 Nov 2019 18:18:07 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 17 Nov 2019 18:18:07 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 064B82CDF98 for ; Sun, 17 Nov 2019 08:10:34 -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=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) Received: from mail-vs1-xe31.google.com (mail-vs1-xe31.google.com [IPv6:2607:f8b0:4864:20::e31]) (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 08:10:33 -0800 (PST) Received: by mail-vs1-xe31.google.com with SMTP id u6so9805543vsp.4 for ; Sun, 17 Nov 2019 08:10:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=NszmwwmTdyos5u0CfJLDTSwGzP5seHoSFhMrlBTnqM4=; b=hdD1Au9cJQZfBJ8KSKLOerdPkCsek1XGVJv7b6EpfAMDiiGijCEBY45UpKY7VfVe+l 1ioBW/FhadDY/SvjFMrIbKn12yJepyw/6WWQPMudJiN7JF4S6bYGrxjXJGaVHYxHQPIJ zycNpLQ/uP30dpIzJapIm8QXIFxTXV9O536Y3UnolRzk01HwNuNpe8yExhC12ftEfSXz PwjhW3ua4vFfb/gy+PHuU+Dpsb8Mgu8jC4or+VYzwOp88Fc+Rt7nhJHb+vmQ1aYH5SBM 3h2mj8qwnuJFR5YvqD7LjYgIHm5o/mxRo2wkmJhzuC1WflRrVM6aadTuoZyCtiM8xqqb GyhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=NszmwwmTdyos5u0CfJLDTSwGzP5seHoSFhMrlBTnqM4=; b=DdrREH8y53pjDbk7a+oBxSzybBNGOn0pTgqvGBafCF323oCizo5FJd7N6fIyOBk+E/ Pb66uHQRdI/UE5+LQ+0b1UFeoCxgtmgdweskZWePvxemQKz4nASOlWCPVDJxJ8W32Fph Q8BQWsQBcBLvbrFU9evFLqz1U5XPnLLCwXo3b85/HNIgz0OVD3WUwP7hiL6HxuZSJ3kX qcfiPJ3okGHPfvahZLYvKDoKoGKrAwgyzLqEAFpykBv2dc3BKVebTuhCCS508h7KvCzd s5rhRbYU40tTwT1yGKIcopLK+LbLNQSq+AlClR/SXir0gkIHn6cKpK500wFVyju3V1Q1 JzMw== X-Gm-Message-State: APjAAAUgWSHZAsSRDcqcVQP1pP8hF54LiH81riJJrWfTu7lxqfirokVG FxjkjjweBZje+QkL9Brl+aF+nTlwz3ryqAs7KAneVw== X-Google-Smtp-Source: APXvYqy3NRnMtLGyumfbRK0MNSpMJqmW2Vk8dFbpO93NoAr4KEDoLCZHVoOuUWOO7HFYbER6PPAV7XAW4T0HIBl1gbA= X-Received: by 2002:a05:6102:36a:: with SMTP id f10mr15963035vsa.44.1574007032870; Sun, 17 Nov 2019 08:10:32 -0800 (PST) MIME-Version: 1.0 References: <461958fe-182e-59bf-9fdb-b110fd09e2b2@aimeos.com> In-Reply-To: <461958fe-182e-59bf-9fdb-b110fd09e2b2@aimeos.com> Date: Sun, 17 Nov 2019 11:10:22 -0500 Message-ID: To: "Aimeos | Norbert Sendetzky" Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000117b6905978d1284" X-Envelope-From: Subject: Re: [PHP-DEV] Concept: "arrayable" pseudo type and \Arrayable interface From: dohpaz@gmail.com (Ken Stanley) --000000000000117b6905978d1284 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, Nov 17, 2019 at 9:42 AM Aimeos | Norbert Sendetzky < norbert@aimeos.com> wrote: > 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 =3D count( $arg ); > $value =3D $arg['key']; > foreach( $arg as $key =3D> $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 =3D new Test(); > $arrayable['key'] =3D $value; > $arrayable =3D useArrayable( $arrayable ); > > And if necessary, we can convert them to native arrays: > > if( $arrayable instanceof \Arrayable ) { > $arrayable =3D $arrayable->toArray(); > } > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php Also the toArray() should be called when an Arrayable is cast to an array: (array)$arrayable. And shouldn=E2=80=99t the toArray match the same naming semantics as __toSt= ring? --=20 "Do not go gentle into that good night. Rage, rage against the dying of the light." =E2=80=94 Dylan Thomas --000000000000117b6905978d1284--