Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58454 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 89671 invoked from network); 2 Mar 2012 04:53:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Mar 2012 04:53:01 -0000 Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.42 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.216.42 mail-qw0-f42.google.com Received: from [209.85.216.42] ([209.85.216.42:50135] helo=mail-qw0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B0/82-11220-C22505F4 for ; Thu, 01 Mar 2012 23:53:00 -0500 Received: by qaui11 with SMTP id i11so81101qau.8 for ; Thu, 01 Mar 2012 20:52:57 -0800 (PST) Received-SPF: pass (google.com: domain of ircmaxell@gmail.com designates 10.224.207.70 as permitted sender) client-ip=10.224.207.70; Authentication-Results: mr.google.com; spf=pass (google.com: domain of ircmaxell@gmail.com designates 10.224.207.70 as permitted sender) smtp.mail=ircmaxell@gmail.com; dkim=pass header.i=ircmaxell@gmail.com Received: from mr.google.com ([10.224.207.70]) by 10.224.207.70 with SMTP id fx6mr95412qab.79.1330663977558 (num_hops = 1); Thu, 01 Mar 2012 20:52:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=uxoeOGZWfB++fmSsjrPX9HvaWwwvrbOguQX8hgM4Z6E=; b=qs5fhRoiLYb/AbdZ5jY+eAPUsK9HtnHI13wG5nM2lQY9S++IiGCzCM9wPfSYwBrtW+ z9sR9xUgiMOFAb53U0eahS0uBix+gXM31mljJ1n+GdGJlqeaTAmOSNtEEq3JiJuXjiYF AwdbFadMTJVlolWOVz9m3auenq8dBaYAKNgAl3+Gl4Jjo5hGPgwwIfyt3af7nLj5Z6eq tmJzJ0ymaUKSOWa15x8exVzWTWyOLwRiLq2NMe/ADuYBABJ0NMxvyLw2MZhqkLV/9tYP 9+k5/GVVXoy+B2QwgwIRwzMmLxtOPH0Fg3G/Y3VI3PbnWhYuI6SogYb4qkRZ5MD2KDKA kWYA== MIME-Version: 1.0 Received: by 10.224.207.70 with SMTP id fx6mr77720qab.79.1330663977502; Thu, 01 Mar 2012 20:52:57 -0800 (PST) Received: by 10.229.233.71 with HTTP; Thu, 1 Mar 2012 20:52:57 -0800 (PST) In-Reply-To: <4F504D4D.6010806@gmail.com> References: <4F504D4D.6010806@gmail.com> Date: Thu, 1 Mar 2012 23:52:57 -0500 Message-ID: To: David Muir Cc: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [POC Patch] Scalar Type Hinting/Casting - Proof Of Concept From: ircmaxell@gmail.com (Anthony Ferrara) Whoops, I linked to the wrong gist... Here's the proper one: https://gist.github.com/1955338 On Thu, Mar 1, 2012 at 11:32 PM, David Muir wrote: > I can't comment on the internal implementation, but I like the use of > the casting syntax. It's not as pretty, but make the intent clear, and > there's not BC issues with class names. > > David > > On 02/03/12 14:48, Anthony Ferrara wrote: >> Hey all, >> >> I know given all the discussion about this topic lately, this is a hot >> topic. =A0But I whipped together a quick POC patch to implement scalar >> type casting for function parameters. =A0Let me describe it: >> >> Patch: https://gist.github.com/1947259 >> >> Example: >> >> function foo( (int) $bar ) { >> =A0 =A0 var_dump($bar); >> } >> >> foo(1); // int(1) >> foo("1"); // int(1) >> foo(1.5); // int(1) >> foo("foo"); // E_RECOVERABLE_ERROR - Expected integer >> foo(array()); // E_RECOVERABLE_ERROR >> >> Right now, I only implemented the checks for (int), but I add the >> parser constructs for (int), (float), (bool), (string) and (object)... >> >> Now, let's talk why I did what I did: >> >> Why did I use cast syntax? =A0Well, there are really three main reasons. >> =A0First off, to indicate that a cast may happen. =A0Second, to prevent >> needing new tokens (and hence reserved words). =A0And third to provide a >> distinction between a string class type hint and a string scalar type >> hint. >> >> Why did I only implement (int)? =A0Well, because I just wanted to build >> a quick dirty POC that can be executed to see the semantics of >> operation. =A0There are issues with it now, so rather than doing all the >> work to re-do it later, I just implemented int... >> >> Why implement (object)? =A0Because right now, there's no way to say you >> want to accept a generic object without caring about type. =A0So the >> (object) cast/hint would then provide that ability to accept a generic >> object. >> >> Why not implement (resource)? =A0Because that would require a new parser >> token, as it's not available now... >> >> How does the casting work? =A0Right now, it's using a copy of the same >> rules that internal functions use with zend_parse_parameters. =A0That >> way, it brings the operating semantics of internal functions and >> userland functions more inline with each other. >> >> >> >> So with that said, there are some (significant) issues with the patch: >> >> 1. First off, the arg checks happen before separation of the zval on >> non-referenced calls. =A0So that means the cast effects the original >> zval AND the argument. =A0Which is a no-go for a production patch. =A0So >> that means that the cast logic would need to be put after the zval >> split. =A0But we'd still want the checks first, so it's not too >> difficult to segregate, just requires deeper changes. =A0It's not >> difficult (that I can see yet), just more work... =A0Example of the >> problem: >> >> # sapi/cli/php -r 'function foo((int) $bar) { var_dump($bar); } $a =3D >> "1"; foo($a); var_dump($a);' >> int(1) >> int(1) >> >> 2. =A0Right now, the zend_aprse_arg_impl ( >> http://lxr.php.net/xref/PHP_5_4/Zend/zend_API.c#zend_parse_arg_impl ) >> that's used by internal functions is defined as static. =A0So we'd be >> copying a lot of the code back and forth. =A0In the production patch, >> I'd also want to re-factor that out a bit into either functions or >> macros to handle the type conversion and casting in both places. =A0That >> way, both systems would behave identical (or as close as possible). >> >> >> So, with that said, what do you think? =A0Is this something worth >> pursuing? =A0Are there any fundamental issues that I'm missing? =A0What >> else would we need to cover in a production patch and RFC? >> >> Thanks, >> >> Anthony >> >