Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108352 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 58569 invoked from network); 4 Feb 2020 09:06:16 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Feb 2020 09:06:16 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C14CC1804DA for ; Mon, 3 Feb 2020 23:18:22 -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-f46.google.com (mail-ot1-f46.google.com [209.85.210.46]) (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 ; Mon, 3 Feb 2020 23:18:22 -0800 (PST) Received: by mail-ot1-f46.google.com with SMTP id r16so16218687otd.2 for ; Mon, 03 Feb 2020 23:18:22 -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=tjHqBvqOZ0CRpysYAt6RmyEiqW7XZIQELZryDG91/SE=; b=t67YKC8v93sDDXrHHccZ4bVUz2ENeuJj0vNJESTEADVIBOi8jG+if9I+qGKhOQ73T/ 8i2Qi3yLGfT+1vDKZvvB1IP/yiwXn/IwzaA4qIU69wILKX2AgT6bTEbSG2CScEYV71Dn 1TwpukVaUMqcYuTahGNH7JHr5OUM3sRbsIIlrz6AuFNMx4uTlT4d+38pqrkVAPvzaZRb on6vffkQb3Bt3/EtSEtL7kWqm5WtDtbTG/R0IQ9k+/kjnE9Cw3nlQljePRdR1mLxAkrp sprWPFxdDQkR9/g3nDhteKQCazDX0BTIvNTahVeFhUBtPuf/Ej2n+N2fiCw0cRnM8HL/ N/UQ== 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=tjHqBvqOZ0CRpysYAt6RmyEiqW7XZIQELZryDG91/SE=; b=e9W7SnVjA9TWYmWqZKicPqe36oCQF2CiwXeyLjJby+0PiQ6ECyMUwho4j5Rx3URjre zhJruNOXtm8bpFUPNMdk7UJGd8FbHghk8I+FQ3FoAjy3Z5SB7PtNFeQSptpq7SU+lFj0 v1/05HwVO3F5stnJ+D0CXNrpz9hrWZzZsUX6l40R1sjAVXU5rVJGxSfwsuTq85xvvJaz AynThQcT48WyD3TT6Xd0nuIUpAistqBSE1ddhpZgXZwMaKfugOMs+yc6VEvFSu8FmJ8N x2e5P7uuvIS0EBBVzoplmymji0WANQiaik0/jG7/fE7ktxKlI2YicV7wphDFneqswnmp O2Hw== X-Gm-Message-State: APjAAAU1wJr3h8eMGhamw2fUZTNIYoZR4pcjp0jzXILn63bA+KRD6FQy 0sW7yNyHJbIIubLkuR3GQ7jg0WceyAt/V6Qyz+aW3leZ X-Google-Smtp-Source: APXvYqwxxSD5vwFCRZtHFM/7wFKoequw71YBaFLgIfyus1LyKBQ5t7vHwuZ3F3J2Vuksb4ExHEdxXTBlHXN2ZOTbQ0w= X-Received: by 2002:a9d:7c99:: with SMTP id q25mr21485840otn.105.1580800701572; Mon, 03 Feb 2020 23:18:21 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 4 Feb 2020 08:18:10 +0100 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="000000000000472a5e059dbad8fe" Subject: Re: What about a magic __toArray() method? From: mtkocak@gmail.com (=?UTF-8?Q?Midori_Ko=C3=A7ak?=) --000000000000472a5e059dbad8fe Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Or we can deprecate __toString() method at all and detect cast events instead. Would it make more sense? Something like this __casted(). P.S: I saw the previous conversation but hence now we have types, it would make sense. Midori On Tue, 4 Feb 2020 at 08:15, Midori Ko=C3=A7ak wrote: > 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 into > 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 to > 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. > --000000000000472a5e059dbad8fe--