简体   繁体   中英

PHP array type hint ignored on my platform

If a PHP function has a parameter type hint (or "type declaration" ) that says "array", and you call this function with another value, eg an integer, there should be a

Fatal error: Uncaught TypeError: Argument 1 passed to foo() must be of the type array, integer given".

Code:

function foo(array $x) {}

foo(5);  // -> Fatal error.

The 3v4l confirms this: https://3v4l.org/7BTtr .
Errors are shown in all relevant PHP versions.

However, I have a local PHP project where the type hint is silently ignored, no error is shown, and subsequent code executes normally.

Some debugging:

  • If I insert the offending code at the beginning of the script (start of index.php), the error is triggered.
  • If I insert the offending code some place later in the script, the error no longer appears.

I imagine there is an ini_set() or something which changes the behavior of PHP towards these errors.

But I don't know which PHP setting, if any, would be responsible for ignoring type errors.

The problem was caused by a custom error handler function. In PHP 5, if the custom error handler does not return FALSE, the script continues running.

The following demo confirms this: https://3v4l.org/neFdl . Look for the results in PHP5!

In my case it was Drupal 7 with _drupal_error_handler(). This function silently ignores the error and does not show or log anything if it the error code does not match the current value for error_reporting() .

This appears stupid, but at least now I know what is happening.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM