Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98542 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64425 invoked from network); 15 Mar 2017 18:39:04 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Mar 2017 18:39:04 -0000 Authentication-Results: pb1.pair.com header.from=narf@devilix.net; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=narf@devilix.net; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain devilix.net designates 74.125.82.170 as permitted sender) X-PHP-List-Original-Sender: narf@devilix.net X-Host-Fingerprint: 74.125.82.170 mail-ot0-f170.google.com Received: from [74.125.82.170] ([74.125.82.170:33260] helo=mail-ot0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 20/E0-38004-74A89C85 for ; Wed, 15 Mar 2017 13:39:04 -0500 Received: by mail-ot0-f170.google.com with SMTP id 19so29244765oti.0 for ; Wed, 15 Mar 2017 11:39:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=devilix.net; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=N2zQAIlaOalHBXT7El/M43I9c78qbhjEZQSj2Iz1kNE=; b=ozbukt2kfj54iZUEch8ul3aitT0wbHMNk+5Zf1DyOWaZ0L1rDLjxeLhmftfjxNnGkH BaeUEq+ufvEwQm9MnruJbTb14OAiMwgxDsqoJJ0aitLuFnzThnJwoOgqG7q3w2+Q40bt qOH9VvqLjrloBuuVibB6EPxHLzI4npWKcAj1c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=N2zQAIlaOalHBXT7El/M43I9c78qbhjEZQSj2Iz1kNE=; b=NCxajehSpCNd6/mJFeiR9V2gt6tq5y2XKyjqPnhBfkB+OMIAS4oT9YDB8lcidG4VwP 3hXStqGiBNQr+6Yu/bdMuRiwbFq2RddXlulRO5zRr0SSSFkLE9o4eAzTLRaqKtx353i4 WpZBjxpGwBTnlnacuppfU8uwQ+alDaHSfPooC+QnCauUY5pfGlv+0XmcWfFPD31crX7a tCe+dLxjOEMo80wdpd43RF4JUjH8RrB973RovDru9XsW4wU4vlS+5axy8OMgUeHQTTgV vLgbX/OcprGZXTgSoR1IblRsrGZ95qEEkpTbwG9+9waa771ajU36nrZG1RijLrjZFBPB JmTg== X-Gm-Message-State: AFeK/H2oCWSvUkcsGNDA0/oviiPUq2CRf5lMyqpOvGeLQqJ3yK8C3SJxjG/UpYg1SH/DKPuQmtTMEFpIFtKDNQ== X-Received: by 10.202.232.210 with SMTP id f201mr2259574oih.60.1489603141153; Wed, 15 Mar 2017 11:39:01 -0700 (PDT) MIME-Version: 1.0 Received: by 10.182.242.83 with HTTP; Wed, 15 Mar 2017 11:39:00 -0700 (PDT) In-Reply-To: References: Date: Wed, 15 Mar 2017 20:39:00 +0200 Message-ID: To: =?UTF-8?Q?Beno=C3=AEt_Burnichon?= Cc: Ryan Pallas , "internals@lists.php.net" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Add __toArray() method to objects that would be called on cast to array From: narf@devilix.net (Andrey Andreev) Hi, On Wed, Mar 15, 2017 at 8:31 PM, Beno=C3=AEt Burnichon wrote: >> I like the idea kind of, but would this remove the ability to cast to >> array all classes not implementing __toArray, as is the case with >> __toString? This would be a HUGE BC if so: >> >> $ php -r 'class Foo {public $foo =3D "foobar";} var_dump((array) (new Fo= o));' >> array(1) { >> ["foo"]=3D> >> string(6) "foobar" >> } >> $ php -r 'class Foo {public $foo =3D "foobar";} var_dump((string) (new >> Foo));' >> PHP Recoverable fatal error: Object of class Foo could not be converted >> to string in Command line code on line 1 >> $ php -v >> PHP 7.1.2 (cli) (built: Feb 27 2017 00:02:44) ( ZTS ) >> > > No. For me, classes not implementing __toArray() should keep current > behavior. That was what I had in mind when I wrote that __toArray() shoul= d > have a default standard implementation. > > Same restrictions could be applied to this magic method: > http://us3.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.= tostring > > By this, I mean, no exceptions should be thrown in this method and return > value MUST be an array. Exceptions cannot be thrown from inside __toString() because that's hard to implement; it's not a "feature" that anybody wanted. Cheers, Andrey.