Strict types for Yii 3

(Pawel) #1

Is there a reason not to use declare(strict_types=1); for Yii 3 other than “that’s a lot of types adding”? :wink:

I can see that neither Symfony nor Laravel are using strict typing as well.


The main reason is that it does not give you much benefit for mature and battle-tested software, and enabling strict types blindly will just break stuff. So your question is fundamentally wrong - you should ask “why (and where) we should use strict types?” instead of “why we should not use strict types?” :smiley:

(Alexander Makarov) #3

We can benefit from more type-hints and these are already used for some classes.

declare(strict_types=1); makes sense for many classes but certainly not controllers where everything inputted is a string.


The same is true for PDO, which also returns string values in most cases, so when working with these strict typing makes things harder.

(Lubosdz) #5

Strict mode is not widely used in applications. So for most of users this would become a blocker because they would need to follow strict types also in their apps, if framework would follow strict types. There are also some critical unsolved situations about strict types like PDO (mysql) returning numeric strings even for integer columns.


Strict mode in framework core classes should not have any effect on applications created using Yii (at least if we do it right).

(Serban Cristian) #7

This has to be a must, not something optional. strict types together with type hinting.
I was hoping all core classes will use type hinting…

(Alexander Makarov) #8

Type hinting added in separate branches that are likely to be merged soon.

(Fsb) #9

Strict type in the framework class files will cause more trouble than it is worth. Way more.

Strict type in my app’s class files has proven to be valuable and we’re using it more and more…

With one nasty exception: action parameters + strict = tears. But this isn’t strictly a strict mode problem, it’s a problem with parameter type declarations. When action parameters were invented in the distant past this was no problem. But now, if you make parameter type declaration a habit, it really is a problem. My preference now is to avoid action parameters in web controllers. We still use them in console controllers.