Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108354 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 62931 invoked from network); 4 Feb 2020 09:13:57 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Feb 2020 09:13:57 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 29290180531 for ; Mon, 3 Feb 2020 23:26:04 -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-oi1-f173.google.com (mail-oi1-f173.google.com [209.85.167.173]) (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:26:03 -0800 (PST) Received: by mail-oi1-f173.google.com with SMTP id i1so17466714oie.8 for ; Mon, 03 Feb 2020 23:26:03 -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=w2sKutQYkLBLmjxjQkhJ/5qaeaDcIkng36vgK13xODg=; b=CHT2+SxBSJxR1ITkFAHLIfiHixp7qSqiju1JA96nCHaJVQpCTt+w8JGiweKW4wZvsB 1OTAHeA+u8xGpXCGcZFh9nuMSB7fveaq+w7HXRvGGXupa/quMUXgpNKdVa/OSg7BS76B bikv4xgWLq+pF/pcsYsgZs6V800DSnV++cGOzXX6p5aexAuoTpXqqyFGuHQdP15Mjike +b8j26Q6G6u7MOL45gx/KtFFU9vrJwLiXTljBjL5r0V9hHGWZrQLTiYzd/w+IXGbUyDB FxisIWo6WvxXIirQZtAuAZc462cCzUfG6udpQwJU3YzRqEBbWoQqQUAU8scrCFh7D07U cuhw== 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=w2sKutQYkLBLmjxjQkhJ/5qaeaDcIkng36vgK13xODg=; b=AZ+DnSOYHnE74dcS7WSspidSL0zZ2+a9PJ8LERAAXEgSu2HpNEKFslwYsA/pS9UR5V XrUzSHNHuracPQEEXIaADAyOHpe7b/oY/HfUKy7Sw6nre4w18wN87r1Hdm+sn7GM3VB+ WL85cFTIyfXGhEkSyszCNDpwg4OISmPeZIWtt/mY0Ki9EYwOvuWsi3Fq4F/dqAGnmvU6 PuXENHXIfkvHhYHPqH7yQmQRXMCL5P9OXsUaju3Leqt0arexs2kyMmE/WW4CYMhQ4wZ+ ClCabYxntJeLuqGHxUsCZsVFZePU83B8bUDpKLbUkeWfg8rh0qyIyce7bkPB/9ctZhJJ IlUg== X-Gm-Message-State: APjAAAXW1SUO2xMGRnpylj6c17Ux2Esb6H2wT4LoyeHX5j1eIQ79Yj8f Vn/skTqoNCg0J6HS9az/NLIyH4RpZfnVEyIsNhdHF9jveHk= X-Google-Smtp-Source: APXvYqy7dXo7L4H4X9tL35RTaYKGxpXK9oDimQdLffq3+DHNENUqYM41e/an4IYn7/Y1yRzTGNwtU510XYb6qtg1xt0= X-Received: by 2002:a05:6808:658:: with SMTP id z24mr2575997oih.91.1580801162298; Mon, 03 Feb 2020 23:26:02 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 4 Feb 2020 08:25:50 +0100 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="000000000000bd491a059dbaf3e1" Subject: Re: What about a magic __toArray() method? From: mtkocak@gmail.com (=?UTF-8?Q?Midori_Ko=C3=A7ak?=) --000000000000bd491a059dbaf3e1 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Oh there is even a rfc. https://wiki.php.net/rfc/to-array What is the status of this? On Tue, 4 Feb 2020 at 08:18, Midori Ko=C3=A7ak wrote: > 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 woul= d > 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 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. >> > --000000000000bd491a059dbaf3e1--