Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:86993 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 15260 invoked from network); 2 Jul 2015 02:28:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Jul 2015 02:28:26 -0000 Authentication-Results: pb1.pair.com header.from=aaron@icicle.io; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=aaron@icicle.io; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain icicle.io designates 199.38.81.6 as permitted sender) X-PHP-List-Original-Sender: aaron@icicle.io X-Host-Fingerprint: 199.38.81.6 mercury.negativeion.net Received: from [199.38.81.6] ([199.38.81.6:63767] helo=mercury.negativeion.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 12/73-20693-4C1A4955 for ; Wed, 01 Jul 2015 22:28:23 -0400 Received: from localhost (localhost [127.0.0.1]) by mercury.negativeion.net (Postfix) with ESMTP id DFD7B275D950; Wed, 1 Jul 2015 22:28:17 -0400 (EDT) X-Virus-Scanned: amavisd-new at negativeion.net Received: from mercury.negativeion.net ([127.0.0.1]) by localhost (mercury.negativeion.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id HeXjnRWjwGsZ; Wed, 1 Jul 2015 22:28:17 -0400 (EDT) Received: from jupiter.local (unknown [173.225.150.231]) by mercury.negativeion.net (Postfix) with ESMTPSA id 2AE34275D943; Wed, 1 Jul 2015 22:28:17 -0400 (EDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2098\)) In-Reply-To: <065901d0b433$b1ef5bd0$15ce1370$@belski.net> Date: Wed, 1 Jul 2015 21:28:16 -0500 Cc: Sara Golemon , pthreads@pthreads.org, internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <1B24FF39-F15D-4506-8A1F-17391517B734@icicle.io> References: <1413875212.2624.3.camel@localhost.localdomain> <5873FC04-0579-41A6-86BD-9755A57CFED5@icicle.io> <065901d0b433$b1ef5bd0$15ce1370$@belski.net> To: Anatol Belski X-Mailer: Apple Mail (2.2098) Subject: Re: [PHP-DEV] [RFC] UString From: aaron@icicle.io (Aaron Piotrowski) > On Jul 1, 2015, at 2:25 PM, Anatol Belski = wrote: >=20 >> Expanding on this idea, a separate RFC could propose a magic > __cast($value) >> static method that would be called for code like below: >>=20 >> $obj =3D (ClassName) $scalarOrObject; // Invokes >> ClassName::__cast($scalarOrObject); >>=20 >> This would allow UString to implement casting a string to a UString = and > allow >> users to implement such behavior with their own classes. >>=20 >> However, I would not implement such casting syntax for UString only. = Being > able >> to write $ustring =3D (UString) $string; without the ability to do so = for > other classes >> would be unusual and confusing in my opinion. If an RFC adding such > behavior >> was implemented, UString could be updated to support casting. >>=20 >> Obviously a UString should be able to be cast to a scalar string = using > (string) >> $ustring. If performance is a concern, UString::__toString() should = cache > the >> result so multiple casts to the same object are quick. > Hi, >=20 > One way doing this is already there thanks > https://wiki.php.net/rfc/operator_overloading_gmp . Consider >=20 > $n =3D gmp_init(42); var_dump($n, (int)$n); >=20 > However the other way round - could be done on case by case basis, = IMHO. > Where it could make sense for class vs scalar, casting class to class = is a > quite unpredictable thing. >=20 > While users could implement it, how is it handled with arbitrary = objects? > How would it map properties, would those classes need to implement the = same > interface, et cetera? We're not in C at this point, where we would = just > force a block of memory to be interpreted as we want. >=20 > Regards >=20 > Anatol Hello, I was thinking that the __cast() static method would examine the = parameter given, then use that value to build a new object and return it = or return null (which would then result in the engine throwing an Error = saying that $scalarOrValue could not be cast to ClassName). It was just = a suggestion to see what others thought because someone suggested = supporting casting syntax such as $ustring =3D (UString) $scalarString. = I don=E2=80=99t really care for either method though (__cast() or = enabling casting just for UString), as they don't offer any advantage = over writing new UString($string) or UString::fromString($string). Aaron Piotrowski=