Php function argument error suppression, empty() i

2020-03-13 05:49发布

I'm pretty sure the answer to this question is no, but in case there's some PHP guru

is it possible to write a function in a way where invalid arguments or non existent variables can be passed in and php will not error without the use of '@'

Much like empty and isset do. You can pass in a variable you just made up and it won't error.

ex:

empty($someBogusVar); // no error

myHappyFunction($someBogusVar); // Php warning / notice

14条回答
smile是对你的礼貌
2楼-- · 2020-03-13 06:32

You can do this using func_get_args like so:

error_reporting(E_ALL);
ini_set('display_errors', 1);

function defaultValue() {
    $args = func_get_args();

    foreach($args as $arg) {
        if (!is_array($arg)) {
            $arg = array($arg);
        }
        foreach($arg as $a) {
            if(!empty($a)) {
                return $a;
            }
        }
    }

    return false;
}

$var = 'bob';

echo defaultValue(compact('var'), 'alpha') . "\n"; //returns 'bob'
echo defaultValue(compact('var2'), 'alpha') . "\n"; //returns 'alpha'
echo defaultValue('alpha') . "\n"; //return
echo defaultValue() . "\n";

This func goes one step further and would give you the first non empty value of any number of args (you could always force it to only take up to two args but this look more useful to me like this).

EDIT: original version didn't use compact to try and make an array of args and STILL gave an error. Error reporting bumped up a notch and this new version with compact is a little less tidy, but still does the same thing and allows you to provide a default value for non existent vars.

查看更多
来,给爷笑一个
3楼-- · 2020-03-13 06:32

There are valid cases where checking becomes cumbersome and unnessesary.
Therfore i've written this little magic function:

/**
 * Shortcut for getting a value from a possibly unset variable.
 * Normal:
 *   if (isset($_GET['foo']) && $_GET['foo'] == 'bar') {
 * Short:
 *   if (value($_GET['foo']) == 'bar') {
 *
 * @param mixed  $variable 
 * @return mixed  Returns null if not set
 */
function value(&$variable) {
    if (isset($variable)) {
        return $variable;
    }
}

It doesn't require any changes to myHappyFunction().
You'll have to change

myHappyFunction($someBogusVar);

to

myHappyFunction(value($someBogusVar));

Stating your intent explicitly. which makes it good practice in my book.

查看更多
放荡不羁爱自由
4楼-- · 2020-03-13 06:32

I'm sure there could be a great discussion on ternary operators vrs function calls. But the point of this question was to see if we can create a function that won't throw an error if a non existent value is passed in without using the '@'

查看更多
Viruses.
5楼-- · 2020-03-13 06:38

Sean, you could do:

$result = ($func_result = doLargeIntenseFunction()) ? $func_result : 'no result';

EDIT:

I'm sure there could be a great discussion on ternary operators vrs function calls. But the point of this question was to see if we can create a function that won't throw an error if a non existent value is passed in without using the '@'

And I told you, check it with isset(). A ternary conditional's first part doesn't check null or not null, it checks true or false. If you try to check true or false on a null value in PHP, you get these warnings. isset() checks whether a variable or expression returns a null value or not, and it returns a boolean, which can be evaluated by the first part of your ternary without any errors.

查看更多
乱世女痞
6楼-- · 2020-03-13 06:40

@Brian: I use a trinary operation to do the check for me:

return $value ? $value : $default;

this returns either $value OR $default. Depending upon the value of $value. If it is 0, false, empty or anything similar the value in $default will be returned.

I'm more going for the challenge to emulate functions like empty() and isset()

查看更多
一夜七次
7楼-- · 2020-03-13 06:41

You don't get any error when a variable is passed by reference (PHP will create a new variable silently):

 function myHappyFunction(&$var)
 {       
 }

But I recommend against abusing this for hiding programming errors.

查看更多
登录 后发表回答