📜 ⬆️ ⬇️

PHP type hinting - strict or soft?

Good afternoon,% username%!

Today I would like to discuss the topic of type hinting in PHP. At the moment in the mailing list for developers there is a heated discussion on this topic and it is discussed which approach should be used and how it will work. Your humble servant is also actively involved in this discussion.

You can start the discussion here and go ahead by clicking on next in thread.
')


Two main implementation options are discussed - strict typing and soft.


Personally, I support the second option. And that's why.

First, this option supports automatic translations of the form integer => float, float => string, string -> float, string => integer. You say, what about “123abc” => int => 123 !? In this case there will be an error. And you need this in order that you would not have to do manual conversion of types each time when we select something from the database. You have to agree, each time explicitly converting record IDs to int is to write very, very much additional code (who does not know, most libraries for working with databases in PHP give all data as strings, regardless of what type of column in database). And that is why I personally oppose strict typing - it will bring more problems than good. Yes, and it will be very difficult to use it because of the very nature of PHP.

In the general case, the mechanism of operation is assumed as follows:


In general, if you have something to say - subscribe to the php-internals mailing list and write to it. If you are too lazy to do it - I can send a message for you.

What do you think about this,% username%? Which approach do you vote for?

UPD1: I will summarize the discussion in the general topic and send it to the internals mailing list. So you have the opportunity to express an opinion as a Russian PHP comunity.

Source: https://habr.com/ru/post/94714/


All Articles