It only works for the file that the declaration is placed in. Meaning that when you use a class, which has the strict_type declaration, it can still result in PHP casting primitive value.
This isn't quite accurate, or at least not the full story.
What strict types enable is type-checking of calls from the file that declares it - even when you call out to other scripts that don't declare it.
Conversely, enabling strict type-checking in your script does not enforce it on other scripts.
What this means in practice, is that library authors can't enforce strict type-checking on calling code - so they can't safely design for the assumption that strict type-checking will happen. Whereas consumers of libraries can't be sure that all library code supports strict types - since the calling script can't selectively enforce type-checks against specific libraries, you may even need to arbitrarily separate calls to different libraries into different files/classes.
It's a very strange and sort-of backwards feature. In most languages, the effects of options like these are localized - but in PHP it kind of spreads out and affects the behavior of code you're calling.
I suspect this design decision stems from the overarching "sanity is optional" philosophy of PHP. 😏
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
This isn't quite accurate, or at least not the full story.
What strict types enable is type-checking of calls from the file that declares it - even when you call out to other scripts that don't declare it.
Conversely, enabling strict type-checking in your script does not enforce it on other scripts.
What this means in practice, is that library authors can't enforce strict type-checking on calling code - so they can't safely design for the assumption that strict type-checking will happen. Whereas consumers of libraries can't be sure that all library code supports strict types - since the calling script can't selectively enforce type-checks against specific libraries, you may even need to arbitrarily separate calls to different libraries into different files/classes.
It's a very strange and sort-of backwards feature. In most languages, the effects of options like these are localized - but in PHP it kind of spreads out and affects the behavior of code you're calling.
I suspect this design decision stems from the overarching "sanity is optional" philosophy of PHP. 😏
This is what I meant with that section of the code, but I couldn't word it right. I'll rewrite it when I'm back from work.
Probably it will be some sort of option to make the whole app strict (being false by default, of course 😂😂)