Extension from https://stackoverflow.com/a/55191/547210
I am creating a validating function to check several attributes of string variables, which may or may not have been set. (One of the attributes which is checked)
What I am trying to do with the function is receive arguments an unknown number of arguments in the form (See below), and suppress errors that may be caused by passing an unset variable.
I'm receiving the variables like validate([ mixed $... ] )
by using func_get_args()
The previous post mentioned that it was possible by passing by reference, now is this possible when the variables are passed implicitly like this?
PHP suppress errors in function parameters
368 views Asked by Olirav At
1
If you pass a variable that is not set in the calling scope, the array returned by
func_get_args()
will contain aNULL
value at the position where the variable was passed, and an error will be triggered. This error is not triggered in the function code itself, but in the function call. There is, therefore, nothing that can be done to suppress this error from within the code of the function.Consider this:
As you can see, the variable name
notSet
appears in the error, telling us that the error was triggered in the caller's scope, not that of the callee.If we want to counter the error, we could do this:
...and prefix the variable names with the evil
@
operator, but a better solution would be to structure our code differently, so we can do the checks ourselves: