Last active
August 29, 2015 14:08
-
-
Save pinepain/9a7755e8ce0d11082047 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// http://php.net/manual/en/language.oop5.typehinting.php says that type-hinted PHP parameter | |
// that has no default value set to null can't be null. But are you sure? | |
// First, register error handler which do nothing and return nothing (this is important!) | |
set_error_handler(function () {}); | |
class SomeClass {} | |
function test(SomeClass $instance) {var_dump($instance);} | |
// NOTE: E_RECOVERABLE_ERROR (4096) occurs here | |
test(null); | |
// But what is this?! We have NULL in output! Are you surprised? |
No i'm not surprised... you register a custom error handler, that doesn't handle the error you are causing, and then you fail to forward it to the default error handler (by returning false
, as the documentation states).
from the docs:
It is important to remember that the standard PHP error handler is completely bypassed for the error types specified by error_types unless the callback function returns FALSE.
add return false;
to the end of your custom_error_handler
as you should, and all is fine.
Thanks all, I have a clue what's wrong with this code. This gist just shows weird behavior I met during debugging strange NULL value for typehinted argument.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You have an error on line 5. It should read: