1

If a PHP function has a parameter type hint (or "type declaration") that says "array", and you call this function with another value, e.g. 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.

3
  • 1
    Does code directly after the line where you expect the error to occur get run? If so, then the error isn't happening, so your type declaration has been ignored. If it does not run, but no error shows, then the error occurs but isn't being reported. Commented Nov 12, 2017 at 23:36
  • The code after the error was still running, producing a complete web page. See my answer. Commented Nov 12, 2017 at 23:39
  • I edited my question to reflect this. Commented Nov 12, 2017 at 23:40

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Seems like a default "Let my page appear slightly broken instead of not at all" function
Yes, but it also suppresses logging of the error. I wonder what's the rationale behind that.
Normally, error_reporting() should only specify which errors are displayed, but even those not displayed should still either crash the page or be logged, or both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.