Typehints for scalar types
An attendee at the PHP New York Conference asked me on some tips on how to implement typehinting for scalar types (integer, float, string, boolean...). In about 20 minutes I explained how it could be done, but I got a bit bored at my flight back so I finished the patch already. The patch is most likely not the best way of implementing this either, and I am also not sure if this patch should be applied to PHP's CVS at all. It might be useful for people that have more control over their PHP setup so I decided to publish it here instead.
Besides the four scalar types it also allows the "object" type hint which will allow objects of any class to be passed. A small example is here:
<?php class blah { } function foo1( object $a ) { } $b = new blah; foo1( $b ); function foo2( integer $i, float $f, bool $b ) { } foo2( 'string', 42, 49.9 );
The example above should show:
Catchable fatal error: Argument 1 passed to foo2() must be of type integer, string given, called in /tmp/test.php on line 16 and defined in /tmp/test.php on line 12
Comments
Will it dynamically cast the string "42" as the int 42?
I would love to see this in PHP 6!
Very cool! Sadly i can't really use it if it isnt either PECL or in PHP.. :(
Btw, is the 'object' typehint really needed? STDClass will also work..
I'm of course against it being applied, except the general object typehint, which is useful.
I don't think so. It might raise a fatal error.
> Btw, is the 'object' typehint really needed? STDClass will also work..
class Foo{} $f = new Foo(); var_dump(($f instanceof StdClass));
result: bool(false)
If you thought StdClass was to PHP what java.lang.Object was to Java, you would be wrong.
I'm sure that Nietzsche would love OO PHP. It is his veiled woman, something completely different on the outside than the inside, always eluding comprehension, always just out of reach. :)
@Hans: StdClass will not work... not all classes inherit from this one in PHP.
@Ben: No, it doesn't cast... the idea of type hints is to prevent wrong data from being passed. It is up to the calling function to provide the correct type.
This patch sounds great. Hope it will be be a regular part of the Engine soon...
By the way I've started to implement a string-class as an extension for php, now i can stop doing this because there is my string typehint :)
Regards
I hope this can be included in PHP6. I think it's a great new feature.
I love it. It should be incorporated into upcoming PHP 5.2
nope, i'm not convinced that this would work, nor that we actually need it in the first place.
sorry, but you should have found something else to do with your 20 minutes ;)
i think this would be great, please add this to PHP!
text/int/float automatically get coerced anyway, but the the type hints would at least improve code readability without developers resorting to Hungarian notation.
type hinting for array and object parameters would provide a definite benefit.
it would be very useful for a function to throw an error if a parameter that should be an array is actually a string. this kind of thing can easily slip by (count/sizeof accept strings iirc) unnoticed for a while. rather than always use is_array(), why not just type hint it, and save everyone redundant code and improve clarity?
Would love to see this in PHP6
array type hinting was introduced with PHP 5.1 (see http://www.php.net/manual/en/language.oop5.typehinting.php)
This is great, thanks alot. I've been waiting for this for quite a while, I was actually working on my own patch when I found this, but compared to yours mine was very bad.
Shortlink
This article has a short URL available: https://drck.me/tfst-4ti