Reputation: 13367
Are object
and array
really the only types allowed for type hinting?
Plus, they state in documentation that standard types string
and int
cannot be type hinted also.
And that makes me more curious. What is the reason behind only allowing two type hints and the dropping of standard ones?
Thanks in advance!
Upvotes: 5
Views: 435
Reputation: 101936
PHP 5.3 currently supports only array
and ClassName
typehints.
The upcoming PHP 5.4 version will also support callable
typehints.
The reason why scalar typehints are currently not supported is that their behavior in a weakly typed language is unclear:
1
and '1'
? -> Against PHP spirit'hallo'
would give 0
for an int
typehint -> UnintuitiveThat's the reason why the scalar typehint implementation was backed out of PHP 5.4 - there was no consensus on this question.
Upvotes: 8
Reputation: 1799
Yes, only array and object are the types allowed for type hinting.
http://www.php.net/manual/en/language.oop5.typehinting.php
See the last line:
Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported.
Upvotes: 0