Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108358 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 23700 invoked from network); 4 Feb 2020 12:18:53 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Feb 2020 12:18:53 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BE28B1804D1 for ; Tue, 4 Feb 2020 02:31:01 -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-f45.google.com (mail-io1-f45.google.com [209.85.166.45]) (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:31:01 -0800 (PST) Received: by mail-io1-f45.google.com with SMTP id t26so20295378ioi.13 for ; Tue, 04 Feb 2020 02:31:01 -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=hSkXZjjR2BmiCoIxBrOk2D86R7TkWFq7bb5vH3G158U=; b=VmQFm0GlxOsqVoXSexyYsfbheVXL6Bb33kUpKixMtrVJGZITZLE4RjjYpu2YnV7Dqg aMyIu4zIqVmixn3liRfMkd4tF4nwrTGMfjHddSVxhJitgSOrnh0lTiU7aDmHxdw9jmsQ EQzSaw7AS4KfpMFtjErmxL96x51z3+xXA5M1A9k+RCV5pt7+WEiXvEDIMZnryavKrDl/ NxBJ3TRLW+wtCrNKIzuO0sBD/dJ3amqjpHUXowx3Skvwwme86zTOy9DfImpxrBvEe4Nv DqsS2BV9pAdUdlcr2+ACqkgkne8klh4s3UozywLXz9x/eGhVzcqc5528TPiEQxVUwMsT Nf/A== 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=hSkXZjjR2BmiCoIxBrOk2D86R7TkWFq7bb5vH3G158U=; b=H/0VA0zN/E5jLjIUqlxXn2X51Wmr2iAx+eCQAfG3ptveoociJLCWqNzJVdiVQD8rYV dy3F0UauxqRlPBsWSQk6VNPR+aYADKbxbXq8ChprNaIqtec+pZ8ZupKPYmogafgATeKs VzdEDPJFfL4YAv51rky1fyXq/SzC1Z4r44qpj/3o1cudY+RDXM+anMOENnQdB3pvcqOx aComBu0AGoiGBHYGQXE4CLAePsrgWH3qaWkI/YtST2ocpFmJCaF020wZvAHPmX1awA6C ZpuDnG/deqXRaIQyqmzYYpOCUetNhW/ltuus1k7tXQH0mW1CUYTgyKq16yyO+RQ0ZX1H RJDw== X-Gm-Message-State: APjAAAWwFPXE1wEDqd5N8/19fvnIRrYxpkPS65EQbRb0JvqUPFgZ531+ p8qw6THTX0amhQVi1/xuZWUCc3o8Sx9mKg7Bod0= X-Google-Smtp-Source: APXvYqxXBtsBNAFz+++Nmo2UWVDeeYBXxsiXcqCzc/1UAJReGcsOx20bnHtadqeSqeoWx6oGIMIA04UnhlicSo8C9+w= X-Received: by 2002:a5d:8f90:: with SMTP id l16mr11650842iol.165.1580812260617; Tue, 04 Feb 2020 02:31:00 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 4 Feb 2020 11:30:48 +0100 Message-ID: To: =?UTF-8?Q?Midori_Ko=C3=A7ak?= Cc: PHP Internals Content-Type: multipart/alternative; boundary="000000000000403385059dbd8942" Subject: Re: [PHP-DEV] What about a magic __toArray() method? From: ocramius@gmail.com (Marco Pivetta) --000000000000403385059dbd8942 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 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. > --000000000000403385059dbd8942--