Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:86201 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 90457 invoked from network); 13 May 2015 21:37:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 May 2015 21:37:25 -0000 Authentication-Results: pb1.pair.com header.from=francois@php.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=francois@php.net; spf=unknown; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 212.27.42.2 as permitted sender) X-PHP-List-Original-Sender: francois@php.net X-Host-Fingerprint: 212.27.42.2 smtp2-g21.free.fr Received: from [212.27.42.2] ([212.27.42.2:47407] helo=smtp2-g21.free.fr) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 86/00-24392-414C3555 for ; Wed, 13 May 2015 17:37:25 -0400 Received: from moorea (unknown [82.240.16.115]) by smtp2-g21.free.fr (Postfix) with ESMTP id 79ADA4B0218; Wed, 13 May 2015 23:36:21 +0200 (CEST) Reply-To: To: "'Guido Contreras Woda'" , References: In-Reply-To: Date: Wed, 13 May 2015 23:37:18 +0200 Message-ID: <023f01d08dc4$fce2e8e0$f6a8baa0$@php.net> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQFuQH5ydiuHZIzbXpHCgxhaceZqP54/AmPg Content-Language: fr X-Antivirus: avast! (VPS 150513-2, 13/05/2015), Outbound message X-Antivirus-Status: Clean Subject: RE: [PHP-DEV] Proposal: interfaces for object to scalar type casting From: francois@php.net (=?utf-8?Q?Fran=C3=A7ois_Laupretre?=) Hi, > De : Guido Contreras Woda [mailto:guiwoda@gmail.com] >=20 > Multiple frameworks and libraries define interfaces or objects that = wrap > around some scalar value and add behavior, the most common case being > arrays and Collection / Hash[Set|List] objects. Although this objects > benefit from the possibility of having behavior themselves, sometimes = the > implementation is outside our project scope (an external dependency) = and > interaction between this object and other blocks of code that expect = scalar > types usually needs to be controlled by the user, which adds the = necessity > for adapters or some sort of mediator. >=20 > Now that we have the ability to hint return types, a common interface = could > be built that, given an object that implements it, lets userland code = use > this object as if it were the scalar type it is wrapping. Unboxing the > object is pretty easy, and I wouldn't go as far as proposing = interfaces for > rebuilding the object itself (not now, at least). >=20 > So, what do you think of a set of interfaces that allow userland = objects to > be used as scalar types? First, we probably don't need anything for arrays. The Iterator = interface family already provides everything we need to consider an = object as an array. For other scalar types, while I agree casting should be supported, I = don't like the solution of creating new methods. I would prefer always = calling __toString() and then, convert the string to the desired type. = So, (int)$object would just be a shortcut for (int)(string)$object. = Quite easy to implement, no new method, just need to enrich the = convert_to_xxx() functions to go through convert_to_string first. I = already had this idea for 7.0 but missed time. Maybe for 7.1. I also think it's more consistent as an object could not be transformed = to different unrelated values when cast to different scalar types. If = $object, when cast to int, becomes 10 and, when cast to string, becomes = 'foo', is it consistent in such a loose typing system ? So, I prefer = that, by construction, (int)$obj always gives the same value as = (int)(string)$obj. Regards Fran=C3=A7ois