Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:86207 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 16451 invoked from network); 14 May 2015 05:43:57 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 May 2015 05:43:57 -0000 Authentication-Results: pb1.pair.com header.from=andreas@heigl.org; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=andreas@heigl.org; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain heigl.org from 195.191.240.18 cause and error) X-PHP-List-Original-Sender: andreas@heigl.org X-Host-Fingerprint: 195.191.240.18 hos109.unaxus.net Received: from [195.191.240.18] ([195.191.240.18:51649] helo=hos109.unaxus.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 71/22-31470-B1634555 for ; Thu, 14 May 2015 01:43:56 -0400 Received: from heigl.gw.tgnet.de ([80.72.250.242]:62778 helo=wdv-hg-0C07-zX4090-HeiglAndreas.local) by hos109.unaxus.net with esmtpsa (TLSv1.2:DHE-RSA-AES128-SHA:128) (Exim 4.85) (envelope-from ) id 1Yslvy-001lKW-RJ for internals@lists.php.net; Thu, 14 May 2015 07:43:51 +0200 Message-ID: <55543615.2030000@heigl.org> Date: Thu, 14 May 2015 07:43:49 +0200 MIME-Version: 1.0 To: internals@lists.php.net References: <023f01d08dc4$fce2e8e0$f6a8baa0$@php.net> In-Reply-To: Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha1; boundary="------------ms020500040201080707050800" X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - hos109.unaxus.net X-AntiAbuse: Original Domain - lists.php.net X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - heigl.org X-Get-Message-Sender-Via: hos109.unaxus.net: authenticated_id: a.heigl+heigl.org/only user confirmed/virtual account not confirmed Subject: Re: [PHP-DEV] Proposal: interfaces for object to scalar type casting From: andreas@heigl.org (Andreas Heigl) --------------ms020500040201080707050800 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi Guido Am 14.05.15 um 01:03 schrieb Guido Contreras Woda: > @francois I understand your point, but I have a different view of the > userland situation. Let me give some examples: >=20 > In the array case, Iterator / Traversable doesn't solve casting, it sol= ves > iteration. > Given a Traversable object from a third party library, if a method from= > another (or the same) third party library requires an array as an argum= ent, > the developer would have to iterate the object and build an array herse= lf > in a Mediator or Adapter class. If she could cast it, then all she woul= d > need to do is orchestrate the messaging, without fiddling in the middle= =2E >=20 > As for the other cases, there are plenty of examples that could prove t= hat > a userland object may want to have a different representation as a stri= ng > than as other scalar types. Money is the first that comes to mind: >=20 > $money =3D new Money(12.5, Money::DOLLARS); > var_dump((string) $money); // string(5) "$12.5" > var_dump((float) $money); // float(12.5) >=20 > While there is information loss in the conversion, it is just a userlan= d > example that could well benefit from different representations in the > various scalar types. For example, if an interest were to be calculated= > from a Money object, the currency wouldn't matter, and the interest > calculator may as well expect just a float as argument. >=20 > Also, there's a huge cognitive gap. For example, if a developer wants a= n > object to be evaluated as a boolean, she would have to force an empty > string as the __toString() method return. That by itself does not repre= sent > her intention. Without proper documentation, it's just a matter of time= for > another developer to find this behavior and remove it or replace it wit= h > something more representative of a string representation of the object.= >=20 >=20 > Cheers! >=20 >=20 For Arrays there are already the ArrayAccess-Interface as well as the ArrayObject-Class which "allows objects to work as arrays" (http://php.net/arrayobject). That ArrayObjects are not covered by the typehint "array" is something that I'm sure can be changed. And whether the typehint "array" covers arrays, ArrayObjects and some special interface that can be implemented by any arbitrary class is open for discussion. But for me the main question is whehter we are just talking about *casting* an object to a certain type or letting an object *behave* like a certain type. Let me rephrase your example: function getprice(int $price) {} $money =3D new Money(12.5, Money::DOLLARS); Will get_price($money) work as the object is cast to int and therefore we have an int in the method getprice? Or will getprice actually use the Money-object but all operations are done against the int-representation? That would also allow something like this function getprice(int $money) { $money =3D $money * CURRENT_VAT; return $money; } which would alter the object in question! Or are we talking about two completely different things? Cheers Andreas --=20 ,,, (o o) +---------------------------------------------------------ooO-(_)-Ooo-+ | Andreas Heigl | | mailto:andreas@heigl.org N 50=C2=B022'59.5" E 08=C2=B0= 23'58" | | http://andreas.heigl.org http://hei.gl/wiFKy7 | +---------------------------------------------------------------------+ | http://hei.gl/root-ca | +---------------------------------------------------------------------+ --------------ms020500040201080707050800 Content-Type: application/pkcs7-signature; name="smime.p7s" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="smime.p7s" Content-Description: S/MIME Cryptographic Signature MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIIFrDCC BagwggOQoAMCAQICAw9P6TANBgkqhkiG9w0BAQ0FADB5MRAwDgYDVQQKEwdSb290IENBMR4w HAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNBIENlcnQgU2lnbmlu ZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRAY2FjZXJ0Lm9yZzAeFw0xNDA3 MjMwOTMyNDlaFw0xNjA3MjIwOTMyNDlaMFwxFjAUBgNVBAMTDUFuZHJlYXMgSGVpZ2wxIDAe BgkqhkiG9w0BCQEWEWEuaGVpZ2xAaGVpZ2wub3JnMSAwHgYJKoZIhvcNAQkBFhFhbmRyZWFz QGhlaWdsLm9yZzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKfpctxYUsO8BeCW VSKQEBTZG/rebtko/5lT/YdNYNbQEnaa4rsYPdwZnFsoqiw7qXo9KqqYupQnwbpv/zmI9CY0 vnP4sb/7C3+nfVI4wBZzlQDsIkBD2mK9QJxX5i3QvOjFY7kwTasRn+KgHa1cyKWQksxTh/kD 2bdhlVIqPfFszusilARmIrROtngxyFPA8x0DAEsHEKgDO1yDhiPiop34hAt9/Qt2Fzne1z1v 5dPtjNYtjmtAocT6E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