Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97252 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 75623 invoked from network); 2 Dec 2016 14:04:37 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Dec 2016 14:04:37 -0000 Authentication-Results: pb1.pair.com smtp.mail=ivan.enderlin@hoa-project.net; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=ivan.enderlin@hoa-project.net; sender-id=unknown Received-SPF: error (pb1.pair.com: domain hoa-project.net from 217.70.183.195 cause and error) X-PHP-List-Original-Sender: ivan.enderlin@hoa-project.net X-Host-Fingerprint: 217.70.183.195 relay3-d.mail.gandi.net Received: from [217.70.183.195] ([217.70.183.195:40538] helo=relay3-d.mail.gandi.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 90/19-01781-47F71485 for ; Fri, 02 Dec 2016 09:04:36 -0500 Received: from mfilter1-d.gandi.net (mfilter1-d.gandi.net [217.70.178.130]) by relay3-d.mail.gandi.net (Postfix) with ESMTP id 50E16A80D7; Fri, 2 Dec 2016 15:04:33 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mfilter1-d.gandi.net Received: from relay3-d.mail.gandi.net ([IPv6:::ffff:217.70.183.195]) by mfilter1-d.gandi.net (mfilter1-d.gandi.net [::ffff:10.0.15.180]) (amavisd-new, port 10024) with ESMTP id f_5cKFtMglKH; Fri, 2 Dec 2016 15:04:30 +0100 (CET) X-Originating-IP: 46.14.238.241 Received: from [10.0.1.147] (241.238.14.46.static.wline.lns.sme.cust.swisscom.ch [46.14.238.241]) (Authenticated sender: ivan.enderlin@hoa-project.net) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id A82B7A80FB; Fri, 2 Dec 2016 15:04:30 +0100 (CET) To: Alex Bowers , PHP References: Message-ID: <8b5c156f-4269-27de-7cc2-9ecebd6a27ad@hoa-project.net> Date: Fri, 2 Dec 2016 15:04:30 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0) Gecko/20100101 Thunderbird/45.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] [Concept] Magic Casting From: ivan.enderlin@hoa-project.net (Ivan Enderlin) Hello :-), Casting is already a very large surface of unsafety and bugs. Adding “magic” before this is like multiplying the surface by infinite. The only goal I see here is to save a `new Collection(…)`, and this is not a sufficient reason from my point of view. If `array` was a type, and `IntoCollection` a trait, implemented for the `array` type, then we could do things like `[1, 2, 3].intoCollection()` for instance. However PHP does not bring this kind of features. So let's reverse this: `Collection::fromArray([1, 2, 3])`. But wait, this is exactly what `new Collection([1, 2, 3])` does for this specific case. Do you have better use cases? A casting comes with a non-negligeable cost. You have to define your data clearly, and avoid casting as much as possible. Cheers. On 02.12.16 14:37, Alex Bowers wrote: > Hello All, > > In PHP we currently have the ability to type hint classes in method > signatures, however, sometimes it would be useful to convert the items to a > different instance / type. > > An example is collections and arrays. If implemented properly, an array and > a collection could be used interchangeably, although a collection may be > preferred. For consistency, having the ability to dynamically cast from an > array to a collection would be very useful. > > An idea of how this may look would be: > > > class Collection extends ... > { > protected $items; > > public function __construct(array $items) > { > $this->items = $items; > } > > ... > > public function __cast(array $items) : Collection > { > return new static($items) > } > } > > -- > > The __cast method MUST return an instance of itself, so that when called > like so: > > function convert_me(Collection $collection) > { > var_dump($collection); > } > > convert_me([1,2,3]); > > the result there would be a Collection instance, instead of an array. > > If the type passed is not accepted by __cast() or __cast() throws an > exception, then the current errors of invalid type are thrown, or perhaps > the custom exception message. > > I'm not sure if this has been proposed before, and if it has, please could > someone assist in me finding it, I couldn't find it by a quick search on > the wiki.php.net/rfc page. > > Thanks. > Alex. >