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've finished reading Children of Memory, the third book in the series.
Another interesting take on forms of intelligent life.
A fourth one is going to get released later this year.
Updated a post_box, a beauty shop, and a restaurant; Confirmed 2 clothes shops, 2 pet shops, and a restaurant
I walked 5.9km in 1h40m39s
Updated a bicycle_parking
Updated 2 waste_baskets
I walked 7.9km in 1h37m12s
Created 3 waste_baskets; Updated 3 bus_stops, 2 benches, and 2 waste_baskets
I walked 8.1km in 1h25m53s
I walked 1.2km in 9m31s
I walked 9.4km in 1h39m05s
Merge branch 'xdebug_3_5'
Merged pull request #1071
Fixed issue #2411: Native Path Mapping is not applied to the initial …
Created 2 waste_baskets; Updated 3 waste_baskets, 2 benches, and 2 other objects; Deleted a waste_basket
I walked 7.9km in 1h45m36s
RE: https://phpc.social/@phpc_tv/116274041642323081
Now that phpc.tv and phpc.social are part of the same umbrella, I've upped my yearly contributions to their Open Collective: https://opencollective.com/phpcommunity/projects/phpc-social
Merge branch 'xdebug_3_5'
Merged pull request #1070
I walked 7.2km in 1h10m26s
Fixed issue #2405: Handle minimum path in .xdebug directory discovery
I've published a new blog post: "Human Creations", on the difference in content generation by LLMs, and the creation of text, art and code by humans.
You can find it at https://derickrethans.nl/human-creations.html or at @blog
I walked 7.8km in 1h38m32s
RE: https://phpc.social/@afilina/116274024588235234
It's good to see that more and more people are realising that the Web can be for-good, without all the enshittification.
That's why I'm happy to see endeavours like phpc.tv springing up, and helping out where I can.
Taking back the control of how the Web is for people, by people, without big tech making it all shit.
Created a waste_basket; Updated 5 crossings and a bicycle_parking
I walked 10.7km in 2h35m10s


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