[POC - Patch] Scalar Type Hinting - A-La zend_parse_parameters
| From: | Anthony Ferrara | Date: | Fri, 09 Mar 2012 02:32:12 +0000 |
| Subject: | [POC - Patch] Scalar Type Hinting - A-La zend_parse_parameters | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-58800@lists.php.net to get a copy of this message | ||
Hey all,
As promised, I've created a POC patch to implement scalar type hints,
the way that zend_parse_parameters handles hinting. First off, here's
the patch:
Directly apply-able without re2c:
https://gist.github.com/2004623
Removing generated files (requires re2c to compile):
https://gist.github.com/2004650
It's a POC, but it mostly works. There is one known issue: passing a
class implementing __toString to a string hinted function will raise a
segmentation fault. There's also an issue of not separating the
argument on cast yielding to reference whether indicated or not, but
that should be easy to fix (it's just issuing a SEPARATE_IF_NOT_REF in
the cases where it would cast)... I'll work on cleaning it up, but I
wanted to show the concept before investing too much work...
So, basically, there are 4 new parameters:
bool
int
float
string
The casting vs error rules are identical to zend_parse_parameters. So:
function fooi(int $i) { var_dump($i); }
fooi(1); // int(1)
fooi(1.5); // int(1)
fooi("1"); // int(1)
fooi("1abc"); // int(1) + notice about non-well-formed numeric
fooi("foo"); // E_RECOVERABLE_ERROR
fooi(true); // int(1)
fooi(array()); // E_RECOVERABLE_ERROR
fooi($obj); // E_RECOVERABLE_ERROR
function foob(bool $b) { var_dump($b); }
foob(1); // bool(true)
foob(1.5); // bool(true)
foob("1"); // bool(true)
foob("abc"); // bool(true)
foob(true); // bool(true)
foob(array()); // E_RECOVERABLE_ERROR
foob($obj); // E_RECOVERABLE_ERROR
function foos(string $s) { var_dump($s);
foos(1); // string("1")
foos(1.5); // string("1.5")
foos("1"); // string("1")
foos(true); // string("1")
foos(array()); // E_RECOVERABLE_ERROR
foos(new StdClass); // E_RECOVERABLE_ERROR
foos($objImpl__toStringORcast_object); // string(result)
Float works like int, so I won't list it out here...
So, what do you think?
Thanks,
Anthony