Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110867 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 7074 invoked from network); 7 Jul 2020 21:34:01 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 7 Jul 2020 21:34:01 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B89DF1804CF for ; Tue, 7 Jul 2020 13:24:44 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 7 Jul 2020 13:24:44 -0700 (PDT) Received: by mail-wr1-f53.google.com with SMTP id k6so46648143wrn.3 for ; Tue, 07 Jul 2020 13:24:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=pmbDvbqi1KEnJCmxrYx5T9zpytlX2ur+La2HYhsdacI=; b=UDYQd4ybjB47Nsov39auszlOFWpxWhxHjVlEBvVbaTcjLAo3wWFjvx4BwAnk98pKVi u5aZ5uFsN8A6zkV2CUYoFJ3mT+BGvUvXGwuURpYMcY+fVnz8FyUTS4+8emNePefvJqps JZSbfwB2Ww4scG0bsJNoV3lKWf8O9ouVmQR7SyegAY7gomlfnIz5bBQuAx81K9qg/3Nw xDsPoEmsquwaPXRQTSA7Rg14yWawZGBHm6AyVL8rrXGc/E3zSIeHb9X4I8EK5pZ+PAU8 fQj+wGGAioz41Sgj+N0Wjd94MnDvrMiYQhyenhxI+vLDyp5d4Qc4Lnf/3CMadw/hRiLi KJOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=pmbDvbqi1KEnJCmxrYx5T9zpytlX2ur+La2HYhsdacI=; b=PMPVKKzIDuacZW53dsf5zNC+6IAh0Ias5y+3azTMgy6Psy/LU8nwAhF0DrbOAvemHv 6wqHTLG5vwG1tB912diAgwBvkhPirXpWnh8xNlPahmF5BIF18zM5mCfbjREBDwdr2VEV TvuaWg/cIomBuMeB/GHhCBZjqVeI/RtPTGAyXxLxv5rMpxKqkSNl3glcK6m7dcFc4Qx+ 3ZhZWDQncSVJJdLWJoDBoTVXGiUeFurWGCH6mj98ZnfbfoIi9ZWE/1/KUJEJ2SdgJNwu JOfWrWcLB6STU1njIl76f8FS1u/oP0xh4oUf20qAZqVVcNPlu/VkI6WmBh7dGdpWFX0d rrAA== X-Gm-Message-State: AOAM533m3uh6d7kjL9TL3VICYKstkVSVnVH7jti1167/6045MmmBEDW5 gKG14w8mj+Tm0QdjECXsrE5L9ymm X-Google-Smtp-Source: ABdhPJwMD3nfb/jlIekpyamrQ7SX/dX1tm/5SfMDwUIZgu3EIvyu3of6mGqDli/lh/EZUO3JT00Nxw== X-Received: by 2002:a5d:4dc2:: with SMTP id f2mr56536182wru.399.1594153482662; Tue, 07 Jul 2020 13:24:42 -0700 (PDT) Received: from [192.168.0.22] (cpc84253-brig22-2-0-cust114.3-3.cable.virginm.net. [81.108.141.115]) by smtp.googlemail.com with ESMTPSA id s8sm2274233wru.38.2020.07.07.13.24.41 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 Jul 2020 13:24:41 -0700 (PDT) To: internals@lists.php.net References: <0431210B-417F-4576-BF28-E51ECFA8F3EF@joshbruce.dev> <15F2E346-4B74-401D-9993-0DFAE8C0A988@joshbruce.dev> Message-ID: Date: Tue, 7 Jul 2020 21:24:39 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <15F2E346-4B74-401D-9993-0DFAE8C0A988@joshbruce.dev> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] Proposal: A way for classes to define a response to any primitive type cast From: rowan.collins@gmail.com (Rowan Tommins) On 07/07/2020 18:13, Josh Bruce wrote: > Decided to put something together:https://github.com/joshbruce/external-project-proposals/blob/master/php-concepts/interact-with-instance-as-php.md Hi Josh, Reading through this, it seems to cover a few different concepts, which are certainly related, but not quite the same. Firstly, we have interfaces such as Countable and Serializable, which don't make the object act like any particular primitive type, they just customise its interaction with particular functions. Iterators could potentially be included in that category, too, because it's fairly easy to imagine the language having multiple primitive types that can be iterated (e.g. separate "list" and "dictionary" types). ArrayAccess feels more explicitly about making an object "act like" an array, but more precisely it overloads the [] operator - it doesn't allow the object to be used in other contexts where arrays are allowed. More general operator overloading was discussed recently, and the particular proposal put forward did not pass its RFC vote. [1]  __toString, on the other hand, only allows the object to be *converted to* a string, not act like one. You can't, for instance, influence the behaviour of the concatenation operator, and make "Hello" . $object . "!" return an object instead of a string, or a string that doesn't begin "Hello". An __toArray method was proposed recently, to mixed reaction. [2] The other closely related topic is what is often discussed as "scalar methods" or "scalar objects" - the ability to call methods on non-object values, and possibly to define your own methods that can be called in this way. This would seem to fit your use case with Shoop quite well: rather than Shoop::array($array)->first()->unfold(), you would just call $array->first(). It's fairly high on a lot of people's wish lists, but there's a lot of details to get right in both design and implementation, so I strongly recommend searching for previous discussions and proofs of concept before diving in. [1] https://wiki.php.net/rfc/userspace_operator_overloading [2] https://externals.io/message/105589 -- Rowan Tommins (né Collins) [IMSoP]