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.
Life Line
I walked 9.1km in 1h35m19s
What new fresh hell is this?
"Please click here and tick the box if you DO NOT want to be opted in."
And when you click on the non-visible link:
"[ ] I DO NOT want to be opted in."
@jamesholden Have you ever seen the Expanse? One of the main characters shares your name!
I walked 2.1km in 21m40s
I walked 6.7km in 1h15m34s
I walked 10.6km in 1h44m34s
I walked 4.4km in 51m58s
I walked 8.2km in 1h40m07s
I walked 2.4km in 38m25s
@Edent With your ActivityPub implementation, have you figured out how to allow quote posts for your bot posts yet?
📷 Brown Cap in the Grass
🚩 Herikhuizerweg, Rheden, Nederland
I walked 0.9km in 11m17s
I walked 2.8km in 25m32s
I walked 4.6km in 1h8m02s
📷 Stalkers Lane
🚩 Graywood Lane, Wealden, United Kingdom
I hiked 23.0km in 4h10m15s
I walked 3.9km in 39m07s
After my PHP 8.5 in Leeds last night, it's now time to head to Rotterdam to give the same talk there tonight!
It's cold out here, but atleast the snow is now gone.
I walked 1.7km in 13m18s




Shortlink
This article has a short URL available: https://drck.me/tfst-4ti