Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:44616 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 91134 invoked from network); 1 Jul 2009 22:39:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Jul 2009 22:39:35 -0000 X-Host-Fingerprint: 99.189.103.1 99-189-103-1.lightspeed.tulsok.sbcglobal.net Received: from [99.189.103.1] ([99.189.103.1:27164] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8C/99-43450-5A5EB4A4 for ; Wed, 01 Jul 2009 18:39:34 -0400 Message-ID: <8C.99.43450.5A5EB4A4@pb1.pair.com> To: internals@lists.php.net Date: Wed, 01 Jul 2009 17:39:35 -0500 User-Agent: Thunderbird 2.0.0.18 (Windows/20081105) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Antivirus: avast! (VPS 090701-0, 07/01/2009), Outbound message X-Antivirus-Status: Clean X-Posted-By: 99.189.103.1 Subject: RFC: Boxing and Unboxing From: spam.goes.in.here@gmail.com (Josh Thompson) Greetings All, Intro ===== The discussion over type hinting seems to be getting divided between those who really like it (most likely the ones who write strongly typed programs anyway) and those who don't want to add yet another kind of type system to PHP. I have been thinking about it and wondered why we don't borrow a concept from C# with a PHP twist of course. The basic idea of boxing in C# is that a value type is implicitly converted to an object type. Unboxing is done explicitly to convert the object back to a value type. Idea ==== I figure instead of adding scalar type hints, we could reuse the object type hints to do implicit boxing of variables. Either (but not both) a new magic method (``__box`` for example) or a new interface (``Boxable`` for example) would need to be added to PHP. The method would accept the scalar value and set its internal representation (similar to ``__wakeup``). The method could either (but not both) return false or throw an exception (``BoxingException`` for example). It is my belief that PHP would need to include as many object versions of scalar types as possible. The boxing functionality could be added to the SPL Types [1]_ to provide for standard representations of the scalar types. However the power to box variables would also be available in userland classes. The unboxing of variables would need to occur on an explicit cast or on a call to an internal function. The unboxing could be implemented as the RFC for class casting to scalar [2]_. It already discusses some examples for a new magic method or interface. I propose some more to relate to the unboxing concept (``__unbox`` for a magic method, and ``Unboxable`` for an interface). Benefits ======== The benefits to boxing and unboxing include: - Only minor modifications required to the current type-hinting implementation (I am only assuming this part). - The boxing and unboxing ability can be used in userland classes to accomplish "cool things". - Get SplTypes and scalar casting in PHP core. - Probably more that I can't think of off the top of my head. Patch ===== I have very limited C skills, so no patch exists. .. [1] `SPL Types `_ .. [2] `RFC: Class casting to scalar