TIL: Using `error_get_last()` in library code is almost always wrong 💥 #php#gotcha
See why this may or may not return a `Warning: Trying to access array offset on value of type null`?
We only call `error_get_last()` if we're sure an error occurred. However, while the error suppression operator `@` correctly hides errors, it still calls global error handlers set via `set_error_handler()` outside our control. In result, `error_get_last()` would return null! 💥
To be safe, correct error handling for functions that may report PHP warnings requires special care: Library code needs to account for any global error handlers by setting up a temporary error handler itself using `set_error_handler()` and `restore_error_handler()` ✅ #php
Given this is pretty common for PHP stream and socket functions, I really wonder if there's an easier way? 👀 #AskingForAFriend
• • •
Missing some Tweet in this thread? You can try to
force a refresh