Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108363 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 43074 invoked from network); 4 Feb 2020 13:07:22 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Feb 2020 13:07:22 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 835CD180210 for ; Tue, 4 Feb 2020 03:19:29 -0800 (PST) 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,HTML_MESSAGE, 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-io1-f54.google.com (mail-io1-f54.google.com [209.85.166.54]) (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, 4 Feb 2020 03:19:29 -0800 (PST) Received: by mail-io1-f54.google.com with SMTP id z8so20495709ioh.0 for ; Tue, 04 Feb 2020 03:19:29 -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; bh=rbYPiyb2uDhcItuVfJGScRtBgf8pi8UU/fCDGUFVYyE=; b=d8rTQ0frvPAopGTpSYd7zfwZgKrcpKfXURUcFtYBF99ZyLQ6xNo4JeRCUY91MhLJkP KaZuxZe5G7HJi03MPFcdpQ9ZDmhK4b0hVc4clGXz1QfUsSsiRh5EgXPNE1AYGAtjA33+ MG0mVVPiKusVzddyKosHljg1h5nlFEX4jCn2nqrxrFpKoWWTxXC/cdwdQoOEKqvk/mVv x0yUICVEpPj2Jm+ZPYrVSbsTt2uupLO+W/7U+B0QctLAU51eENnk+ODOrmZbjPSqPd6N HCEOexKrDg1+qa2Vc4F1A8jllTWL4AD/o76H1gI8posZag9LL5pR3l087Of4e1fU6dKP +W4g== 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; bh=rbYPiyb2uDhcItuVfJGScRtBgf8pi8UU/fCDGUFVYyE=; b=VjFmeGtxhYGsTJNlYWxX6o2ciKA5hwzwidcyCn37QYr4kjn8I86byac7o8Seti+fEZ 0SV/cGlG13zTnB5EBf440B4auOq8H9xRbFPZyhl73hE1DjCQtVG1LbtfRd/d4l+Urcql FRFhJp3GNy9Wedx9uQyfdpJBViUGutJEF5gjJGdjKJ0LuiwDUHRhrJWNE9VFOGglb+BF 9mD9L+keh56w9u1iZ3qyL745TxDI14gy+OnGvWJx7DLhqZ6X8lsgrbn3qVoQ1ObCzpXG E25HI4bszO9PbxC/7AJZkWkMOhA8UIi9m5e5PKO3M4GraPDOfMdI3Wm0Ae+wa30IRvKO wEYw== X-Gm-Message-State: APjAAAXwp5qGzE8SmnZsbFeIH+rxBUKDlgHjxucj544iKsU2g+X2ErH/ bJwu5nDQ3Tof4lYahE3oC60VXVKMZ7euQnSblQU+tjxy X-Google-Smtp-Source: APXvYqyAJVWGwMNTT3+1y8JoXdbUYbwP3o7rJS98kUn2+DTlufODHypHtiKXCMXejnTpk2aemXFxODALb7b8abcBZrM= X-Received: by 2002:a6b:ec0f:: with SMTP id c15mr22027182ioh.149.1580815167635; Tue, 04 Feb 2020 03:19:27 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 4 Feb 2020 11:19:16 +0000 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="00000000000085c38b059dbe364b" Subject: Re: [PHP-DEV] What about a magic __toArray() method? From: rowan.collins@gmail.com (Rowan Tommins) --00000000000085c38b059dbe364b Content-Type: text/plain; charset="UTF-8" On Tue, 4 Feb 2020 at 10:37, Marco Pivetta wrote: > Correct: an explicit `$foo->toArray()` call suffices. > > For other readers, more about `$foo->toArray()` vs `(array) $foo` is > described at > > https://github.com/ShittySoft/symfony-live-berlin-2018-doctrine-tutorial/pull/3#issuecomment-460441229 > The pros and cons of overloading casts are roughly the same as those of operator overloading in general: on one hand: + It is more succinct than calling a method + It aids creation of richer Domain Specific Languages on the other: - It is less explicit than choosing a purpose-specific method name - It is less flexible than having multiple related methods, or a method with parameters - It is harder to predict behaviour at a glance > Or we can deprecate __toString() method at all and detect cast events > instead. Would it make more sense? Something like this __casted(). If there was a good case made for how it should work, I'd be more supportive of a general feature to overload casting, rather than making (array) a special case. One of many challenges would be working out when the cast should happen implicitly. Should "iterable $foo" trigger an array cast if the object isn't iterable? Should foreach()? Should strict_types change that behaviour as it does for __toString(), even though it currently only applies to scalar types? Regards, -- Rowan Tommins [IMSoP] --00000000000085c38b059dbe364b--