pass an argument that may not be set to a function, without throwing a notice

元气小坏坏 提交于 2019-12-20 04:20:44

问题


PHP has the isset() function, that takes a variable, and tells you whether it was set. It doesn't throw a notice when passed an array key that doesn't exist. I'd like to duplicate that functionality in a new function that does something like this:

function my_function($arg1, $arg2) {
    if(isset($arg1)) {
        //...do something
    } else {
        //...do something else
    }
}

or eventually something like this:

function my_function() {
    $arglist = func_get_args();
    foreach($arglist as $arg) {
        if(isset($arg)) {
            // ... do something
        }
    }
}

Obviously, these functions won't work. I could suppress all errors with @, and then just test for null, but I'd prefer to leave error reporting intact.

Any ideas?


回答1:


You can assign default values to the parameters and then check to see if they are set to their default values when the function is called. In this case I used null:

function my_function($arg1=null, $arg2=null) {
    if(!is_null($arg1)) {
        //...do something
    } else {
        //...do something else
    }
}



回答2:


function my_function($arg1, $arg2) {

Inside this function, $arg1 and $arg2 will always be set, because you declared those variables in the function signature. Checking for isset inside this function is pointless.

When calling this function, you're passing some completely different variable into it:

my_function($foo, $bar);

I'm guessing you want to avoid having to check whether $foo or $bar are set in the calling scope. This is not possible. $foo is a different variable than $arg1. You need to check whether $foo isset in the scope in which $foo is supposed to exist, my_function has nothing to do with that scope.

function my_function($arg1) {
    if (isset($arg1)) ...
}

my_function($foo);
              ^
              |
              +--- error is thrown here, code inside my_function can't help

There may be all sorts of elaborate ways to avoid having to write isset, but don't go there. Write isset where it's necessary, avoid having to write isset by properly declaring your variables where they should exist.

my_function(isset($foo) ? $foo : null);

In fact, the only time you really can't know whether entire variables are set is for user input, mostly through $_POST or $_GET. For those you have to check. Any other variable inside your application you have full control over and can ensure that they exist where they should exist.




回答3:


So, after extensive fooling around, based on the answers given, I found a solution!

The issue is one of scope, and the error-checking occurs at the moment of assignment, when you call a function with values.

HOWEVER

If you pass your variables by reference, error checking is not triggered! In fact, the references are passed, and errors are thrown at the first bad use, inside the function!

// returns an HTML-safe version of the first defined, non-falsey argument
function print_OR(&$highp = null, &$lowp = null, &$default=null) {
    if(isset($highp)&&($highp||$highp===0)) return htmlspecialchars($highp);
    if(isset($lowp )&&($lowp ||$lowp ===0)) return htmlspecialchars($lowp);
    return htmlspecialchars($default);
}

Here are a set of tests, showing this as functional: http://pastebin.com/C9QTvSi7



来源:https://stackoverflow.com/questions/22637817/pass-an-argument-that-may-not-be-set-to-a-function-without-throwing-a-notice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!