Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type hinting and the reason(s) behind their limitations

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!

like image 746
tomsseisums Avatar asked Oct 20 '11 14:10

tomsseisums


1 Answers

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:

  • Should an error be thrown, even if it's just the difference between 1 and '1'? -> Against PHP spirit
  • Should normal PHP casts be applied? In that case 'hallo' would give 0 for an int typehint -> Unintuitive
  • Should some more strict casting rules apply?

That's the reason why the scalar typehint implementation was backed out of PHP 5.4 - there was no consensus on this question.

like image 108
NikiC Avatar answered Sep 23 '22 14:09

NikiC