|
|
|
|
|
by makeitdouble
1252 days ago
|
|
Yes, except it has to be set on the _caller_ side. I kinda see why, after all it’s the caller who will deal with the TypeError. But assuming we’re not setting types for all our functions, when I do for a specific one, I want to enforce that strictness on the _callee_ side (“for this function, it really matters that the parameters are correct”), and not have to go check if every single caller files properly has the strictness set. [0] So in the end, the best option is to _not_ type scalar parameters, and do the strict check manually and throw your own TypeError, inside your function instead. [0] Auto setting strictness for every file in your project and checking for it in CI clears the issue, but that becomes another boilerplate you’re adding to your system. And it still doesn’t work for native functions. It’s so close. Really, so close to be good. |
|
There was this RFC[0] but it seems to have fizzled.
> And it still doesn’t work for native functions.
The page states: 'Function calls from within internal functions will not be affected by the strict_types declaration' (emphasis mine). Outside of array_map I don't think this happens all that much.
> So in the end, the best option is to _not_ type scalar parameters, and do the strict check manually and throw your own TypeError, inside your function instead.
That sounds awful. Why not install a nice static analyzer like phpstan or psalm and never think about it again?
[0]: https://wiki.php.net/rfc/namespace_scoped_declares