Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:47838 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 44177 invoked from network); 8 Apr 2010 22:08:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Apr 2010 22:08:56 -0000 X-Host-Fingerprint: 98.189.173.115 wsip-98-189-173-115.oc.oc.cox.net Received: from [98.189.173.115] ([98.189.173.115:2584] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CC/E0-54833-6F35EBB4 for ; Thu, 08 Apr 2010 18:08:55 -0400 Message-ID: To: internals@lists.php.net Date: Thu, 08 Apr 2010 15:08:36 -0700 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.9) Gecko/20100317 Lightning/1.0b1 Thunderbird/3.0.4 MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 98.189.173.115 Subject: Autoboxing with type hints From: jeremy@pinacol.com (Jeremy) I'm not sure if this is a bug, an oversight, or something that is known and deemed not-worth-fixing, so I thought I would ask here first. Consider the SPL_Types extension. This extension introduces some classes that override assignment in order to do some type-checked autoboxing: $int = new SplInt; $int = 5; //$int is not overwritten with scalar '5', but is now SplInt(5). $int = 'foo'; //throws UnexpectedValueException: Value not an integer That is a pretty neat thing. However, I would see the primary use-case for this as the following: function foo(SplInt $int) { //some code } foo(5); Unfortunately, this produces "Catchable fatal error: argument 1 passed to foo() must be an instance of SplInt, integer given". This, however, works: $int = new SplInt; $int = 5; foo($int); Does it seem like a good idea for the type checking mechanism for type hinted parameters to check whether the hinted class has overridden assignment, and if so, delegate the type checking to that mechanism? For example, foo('bar'); would yield 'UnexpectedValueException: Value not an integer', rather than the catchable fatal error associated with the type hinted parameter. Or, perhaps there is something that the extension itself could be doing to make this happen? Or, perhaps this intentionally does not happen? I know that genuine scalar type hints are kinda-sorta-maybe on the table for a future version, but as of right now using the SPL Types to emulate that in a performant way would be pretty neat. Thanks, Jeremy P.S. - As a side note - I wonder whether it would be possible to make accessible from userspace the functionality that SPL_Types uses to override assignment? For example, you could have an __assign($value) magic method that, if present, would be invoked when attempting to assign a value to an existing object: class Foo { public function __assign($value) { echo $value; } } $foo = new Foo; $foo = 'foobar'; //outputs foobar echo ($foo instanceof Foo); //output 1 That's not something I'm really requesting - just an interesting thought. I'm sure that operator overloading has been brought up and shot down before, and that functionality is dangerously close to such. But yeah.