Type declarations are a simple programming concept that lots of developers use on a daily basis. In the code example below we see a basic method th...
For further actions, you may consider blocking this person and/or reporting abuse
Even though I sometime use type declarations, I try to prevent myself to start using them in every single scenario. An Example of where I mostly reach for type declarations is this:
I think in cases like the top one it helps for readability.
I feel sometimes that when I reach for so much type hints and return types, I'm making PHP something more Like Java (And I really don't enjoy Java).
It's just my way of thinking. Totally respect your point of view also. 😊
I think this often depends on your code background. I got into PHP after I'd written more strongly typed languages like c#. So I've always felt comfortable around type hints, etc.
Absolutely agree with you.
then how can you tell what to pass to functions?
lol, your talking like the developer is a kid. I’m expected to have a certain domain over the codebase. 😁
Until you don't.
This is debatable.
Have you looked into using Haxe as a possible way to have strict typing while still being able to target PHP?
Granted type checking only happens during compile time and generated PHP code might be harder to read, but Haxe adds a lot more than just type checking.
So if you've never heard of Haxe, it might be worth checking it out.
Disclaimer: Despite my user name, I'm not connected to the makers of Haxe, but I'm using Haxe for my past, current and future PHP projects.
Thanks for the suggestion I'll give it a look.
I think you got the per-file basis backwards: strict types must be declared in the file calling the function, not the file where the function is defined.
From the PHP manual:
See php.net/manual/en/functions.argume...
Sadly, I think this partly defeats the purpose of enforcing types, since having to remember to declare strict types in each file that calls a function is not that different from just getting the function calls right in the first place.
Yes what the manual says is sort of right on parameter types. If you don't declare strict types then the calling code will fall back to type coercion and not impose the parameter types defined in the required file. But if it can't coerce to the defined parameter type the code will still fail.
I've updated the associated GitHub library to highlight this point. github.com/RobDWaller/type-declara...
The article itself though is still technically correct it's just it is focused on return types and the fact you can't trust what you receive from a method without strict types imposed. I should maybe have stressed this point more.
I agree though that PHP Types aren't ideal but do believe they still have value when used correctly, you just have to impose strict types everywhere. 😂
"This approach does have some consequences though." - it's not this approach though.
Type coercion is what PHP always does by default. Basically, to make it simple, strict types means PHP won't do coercion when types are declared and will perform the default coercion behaviour otherwise.