Skip to content

Instantly share code, notes, and snippets.

@pinepain
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinepain/9a7755e8ce0d11082047 to your computer and use it in GitHub Desktop.
Save pinepain/9a7755e8ce0d11082047 to your computer and use it in GitHub Desktop.
<?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?
@Bilge
Copy link

Bilge commented Oct 29, 2014

You have an error on line 5. It should read:

// Here is some code that I have no idea what it does and, thanks to this comment,
// neither do you.

@ctrl-f5
Copy link

ctrl-f5 commented Oct 29, 2014

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.

@pinepain
Copy link
Author

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