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
In more info innocent times, the hat riots caused some spankings...
100 Years Ago Men and Boys Fought on the Streets of New York Over Wearing Straw Hats Past Summer | The New York Public Library
https://www.nypl.org/blog/2022/09/23/straw-hat-riots-nycI hiked 15.5km in 3h12m14s
š· Mushroom Village
š© Weg van den Prins Willemsberg, Ellecom, Nederland
I walked 2.4km in 59m34s
š· National Cycle Network 6
š© Watford, United Kingdom
I walked 7.6km in 1h54m50s
š· Tufted Duck Pair
š© Outer Circle, City of Westminster, United Kingdom
RE: https://phpc.social/@Xdebug/115662135830755552
I have just released Xdebug 3.5.0!
In the next few weeks I will create some content (text, and perhaps video) highlighting some new features in more detail.
Please share it with the world!
The master branch is now for Xdebug 3.6, targetting PHP 8.6
Back to -dev
Tweak release instructions a little
Go with 3.5.0
Tweak message IDs and severities for control socket log entries
I walked 8.1km in 1h26m40s
I walked 1.1km in 9m28s
I walked 8.5km in 1h30m56s
My whisky of the month for December 2025, is a 15yo Aultmore bottled by Cadenhead's.
Fixed off-by-one error in address length name for control socket on Lā¦
Merged pull request #1050
Fixed control socket name by removing silly trailing things (by not pā¦




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