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 know my French is pretty terrible, but I'm sure I'm closer to the correct answer than what's shown here...
Merge branch 'v2022'
Merge pull request #169 from psumbera/solaris-2
I walked 7.0km in 1h6m48s
Fixed some ffing sidewalks again.
I walked 10.5km in 1h40m26s
Updated a pet_grooming shop
I walked 8.6km in 2h12m58s
I walked 8.7km in 1h24m16s
Updated a restaurant
I walked 2.4km in 24m20s
I walked 6.6km in 1h4m32s
I walked 0.6km in 4m38s
I walked 8.5km in 1h22m35s
Merged pull request #1029
Reflow some comments
Add comments, add end of file newlines, fix php 8.5 compilation
Benchmark Xdebug performance
Merged pull request #1051
PHP 8.6: printf() is now optimised out if it only uses %s and %d (and…
PHP 8.6: The object IDs of objects changed in tests
PHP 8.6: ZSTR_INIT_LITERAL() no longer takes non-literals, so use zen…
PHP 8.6: WRONG_PARAM_COUNT has been removed in favour of zend_wrong_p…
PHP 8.6: zval_dtor() has been deprecated in favour of zval_ptr_dtor_n…
Update test for version constraints, as well as the error messages


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