PHP 5.3 with improved Type Hinting and type casting
From Illia Alshanetsky blog post:
There has been a lot of comments both on this blog and the internals list. There seems to be a fairly large group of core developers who like the idea as well as surpassingly large support base on the user level too (wow, didn’t think that this many people want type hinting). Unfortunately, there have also been, as is typically on the internals list a few people complaining for the sake of complaining. Their arguments have ranged from type hinting is against PHP’s loosely typed nature and people will mis-use it and make PHP into something that it is not, to I don’t need or will use it, so no one should get it.
…Here is the quick changelog.
- Added support for “object” type hint
- Modified the patch not to break binary compatibility so you can now use patched PHP without having to re-compile your extensions or having issues with binary ones.
- Added full reflection support, which I appropriated from Felipe’s earlier work on type hinting
- Added support for type casting, where by you can do things like this function a((int)$foo), which will force PHP to cast the value of $foo to an integer
- Added tests, which again I partially appropriated from Felipe’s earlier work.
The types that could be used (according to the proposed patch) are:
| Boolean | String | Integers | Floats | Others |
|
|
|
|
|
So, how do that will look like? Here some examples of how this will look like:
Type Casting on function definition:
function printMe((int) $value) {
var_dump($value);
}
// Output:
// int(50)
printMe(50);
// Output:
// int(123)
printMe('123');
// Output:
// int(0)
printMe('Testing');
// Output:
// int(0)
printMe(null);
Or using String as type casting
function printMeAgain((string) $value) {
var_dump($value);
}
// Output:
// string(2) "50"
printMeAgain(50);
// Output:
// string(3) "123"
printMeAgain('123');
// Output:
// string(7) "Testing"
printMeAgain('Testing');
// Output:
// string(0) ""
printMeAgain(null);
Type Hinting
function test_int(int $a = 1) {
var_dump($a);
}
// Output:
// int(1)
test_int();
// Output:
// int(123)
test_int(123);
// Output:
// Catchable fatal error: Argument 2 passed to test_int()
// must be of the type integer, string given
test_int("123");
With doubles:
function test_double(real $a) {
var_dump($a);
}
// Output:
// float(1)
test_double(1);
// Output:
// float(1.123)
test_double(1.123);
// Output:
// Catchable fatal error: Argument 1 passed to
// test_double() must be of the type double,
// string given.
test_double("123");
With Strings:
function test_string(string $a) {
var_dump($a);
}
// Output:
// string(10) "Hola Mundo"
test_double("Hola Mundo");
// Output:
// Catchable fatal error: Argument 1 passed to
// test_string() must be of the type string,
// int given
test_double(123);
// Output:
// Catchable fatal error: Argument 1 passed to
// test_string() must be of the type string,
// null given
test_str(null);
-
Greg Ambrose
-
Alexandr

Follow me on Twitter
RSS