3

I have a function that takes just 1 parameter, but i passed it 2 parameters by accident. It failed silently. I didnt realize that the 2nd parameter was being ignored so it took a while to debug.

Is there a way to turn on error checking for a such a condition?

user2864740
  • 60,010
  • 15
  • 145
  • 220
glutz
  • 1,889
  • 7
  • 29
  • 44

2 Answers2

2

I suspect it is not possible to globally cause an error when more arguments are passed than are specified in the function signature, because PHP supports a variable number of arguments in user-defined functions by way func_get_args(), for example. PHP will report an error (of level E_WARNING at the time of writing this answer) if you call a function with less arguments than specified in the signature, but it does not report an error if you call the function with more arguments than specified in the signature.

It's not quite what you're specifically asking for, but of course you can write code to enforce exactly 1 parameter if you absolutely need this behaviour.

function foo($only_param) {
   if (func_num_args() > 1) {
      trigger_error(__FUNCTION__ . "(): Too many params", E_USER_ERROR);
   }
   ...
}
faintsignal
  • 1,828
  • 3
  • 22
  • 30
-1

Set this

error_reporting(E_ALL);

Or create your own error handler with

set_error_handler('customHandler')

and a function like this

function customHandler( $error, $string, $page, $line, $contextArray )
{
     //$error will be PHP name (E_ERROR, E_NOTICE, etc)
}
John Smith
  • 490
  • 2
  • 11
  • 1
    Code should work seemingly with E_ALL unless you are a sloppy coder. It is not recommended for production in the event you missed something on development. – Aziz Saleh Apr 14 '14 at 01:41
  • @AzizSaleh I would say it (E_ALL) is *most definitely* recommended in production, with the right configuration - http://stackoverflow.com/questions/22662488/bind-param-between-environment/22662582#22662582 – user2864740 Apr 14 '14 at 01:53
  • Updated. Anyone get anywhere? – John Smith Apr 14 '14 at 01:56