Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108360 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27865 invoked from network); 4 Feb 2020 12:23:34 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Feb 2020 12:23:34 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7E2A2180211 for ; Tue, 4 Feb 2020 02:35:42 -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_H3,RCVD_IN_MSPIKE_WL,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-ot1-f50.google.com (mail-ot1-f50.google.com [209.85.210.50]) (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 02:35:42 -0800 (PST) Received: by mail-ot1-f50.google.com with SMTP id g64so16574954otb.13 for ; Tue, 04 Feb 2020 02:35:42 -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=zcMDW/6l+qGYYsWwgR0r243zjFwA+yx+bnoHU5Tlr6g=; b=s8PQwT55OgMMUdf/5dKoSXfiupMQ9w8p/CprP8Hp74bvqlqAGT++XPk8fFTa92VHlP GEV1FYt+vrEMEBBFYshUbh1928w1ABM1gbaXluGD100osxMrfdyKV+SW2ujG7G+r7Dq0 vVyItK8kVNKy9LnNxclExmm7zlMkGH8YYszWcuWxpBDMOs0yELpqdf7z1bj8zVMaMAn0 8AMU65JhY9yZKuIJKum/PqDeoFsA4wn2g8uBvrVawwXaUqjlz5VrDIscuEXR+K6VZAC5 k0hJ4FfDHKBJ0nYdX/Yc/jlgRyURHoxYVgUSMHzsbs/v6NHNWA/3oMwYS1klKxr0haNF iUAQ== 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=zcMDW/6l+qGYYsWwgR0r243zjFwA+yx+bnoHU5Tlr6g=; b=tdxXYdcvEAkttIHQTyfAQfmg6fc3l8U8tgYmbabzI4ZF9Wol0auJC5ec6uiyEFk+Kk K/nPUOc2tsRko8feKdiKWoZw6NlA4pg9rKcYfOgtFZQKpVd1brD/dSGbCLEpBrTUDbfF A22BfTft0XQAbRX0k7bLsGyvBE2SX76QKE6AEOLxPTmGwUyG8PmJ4jVytjTbZNSNjAih cAoml6hX0FnA8vHJsVJWNyHYuHMXNb044TjbNciLiIxw/Tb3nTjMr43+jZCYtvBqOns/ XsIVxnsLPjBRfuNjsrrSA/Ky8xMyPOrKrbAJX9Hk9HnmfAKXNBlS4YwGKvQceoQP5TRX dChw== X-Gm-Message-State: APjAAAX8jhWZ4+kc6kWzLhIBdDhYgfhAVeLYjJaBE5Y1ZIqNWvGt0kF5 TAshUbawdqviyRNbmPq0/7wdlEJFYmT9UkG4Y+Q= X-Google-Smtp-Source: APXvYqykSekecMdWq6Sy+KJvRCXdTzRLPTDjrDbqUZUX0euWD9h6n8EeQpA245JBXCv5M0OaXx3eeAPO3PDL+XKCeu8= X-Received: by 2002:a05:6830:50:: with SMTP id d16mr14814782otp.166.1580812540462; Tue, 04 Feb 2020 02:35:40 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 4 Feb 2020 11:35:29 +0100 Message-ID: To: Marco Pivetta Cc: PHP Internals Content-Type: multipart/alternative; boundary="000000000000ee4c5c059dbd99cf" Subject: Re: [PHP-DEV] What about a magic __toArray() method? From: mtkocak@gmail.com (=?UTF-8?Q?Midori_Ko=C3=A7ak?=) --000000000000ee4c5c059dbd99cf Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Marco, I've read it and your argument is strong. I think my opinion slightly moves towards Andreas, away from magic. here is something I wrote some days ago: https://github.com/midorikocak/arraytools/blob/master/src/ArrayConvertableT= rait.php I guess you were saying something like this would be enough. Midori On Tue, 4 Feb 2020 at 11:31, Marco Pivetta wrote: > Hey Midori, > > This has been discussed in more depth 2 years ago: > https://externals.io/message/98539#98539 > > In practice, the `(array)` cast is one of the few operations that don't > cause observable side-effects in this programming language: let's please > keep it as such. > > Marco Pivetta > > http://twitter.com/Ocramius > > http://ocramius.github.com/ > > > On Tue, Feb 4, 2020 at 8:15 AM Midori Ko=C3=A7ak wrot= e: > >> As you know we have __toString method that runs whenever an object is >> typecasted to string or it is directly echoed. >> >> > >> $class =3D (new class{ >> public function __toString(){ >> echo "casted\n"; >> return "mahmut\n"; >> } >> }); >> >> echo $class; >> $casted =3D (string)$class; >> >> /* >> prints: >> casted >> mahmut >> casted >> mahmut >> */ >> >> >> As you know toArray() method implemented when an object is converted int= o >> and array and most of the time when a plain data object is sent to >> front-end. >> >> Having a magic method like __toString called __toArray would be useful t= o >> detect and act on conversion events. >> >> Roughly it would be like: >> >> > >> $class =3D (new class{ >> public function __toArray(){ >> echo "casted\n"; >> return >> [ >> 'key'=3D>'value' >> ]; >> } >> }); >> >> >> $casted =3D (array)$class; >> print_r($casted); >> >> /* >> prints: >> Array >> ( >> [key] =3D> value >> ) >> mahmut >> */ >> >> What would you think? I think it would add value. >> > --000000000000ee4c5c059dbd99cf--