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条回答
闹够了就滚
2楼-- · 2020-03-13 06:23

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 '@'

Yes you can!

porneL is correct [edit:I don't have enough points to link to his answer or vote it up, but it's on this page]

He is also correct when he cautions "But I recommend against abusing this for hiding programming errors." however error suppression via the Error Control Operator (@) should also be avoided for this same reason.

I'm new to Stack Overflow, but I hope it's not common for an incorrect answer to be ranked the highest on a page while the correct answer receives no votes. :(

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

@Sean That was already answered by Brian

return isset($input) ? $input : $default;
查看更多
Explosion°爆炸
4楼-- · 2020-03-13 06:27

And going further up the abstraction tree, what are you using this for?

You could either initialize those values in each class as appropriate or create a specific class containing all the default values and attributes, like:

class Configuration {

    private var $configValues = array( 'cool' => 'Defaultcoolval' ,
                                       'uncool' => 'Defuncoolval'  );

    public setCool($val) {
        $this->configValues['cool'] = $val;
    }

    public getCool() {
        return $this->configValues['cool'];
    }

}

The idea being that, when using defaultValue function everywhere up and down in your code, it will become a maintenance nightmare whenever you have to change a value, looking for all the places where you've put a defaultValue call. And it'll also probably lead you to repeat yourself, violating DRY.

Whereas this is a single place to store all those default values. You might be tempted to avoid creating those setters and getters, but they also help in maintenance, in case it becomse pertinent to do some modification of outputs or validation of inputs.

查看更多
欢心
5楼-- · 2020-03-13 06:30

If you simply add a default value to the parameter, you can skip it when calling the function. For example:

function empty($paramName = ""){
    if(isset($paramName){
        //Code here
    }
    else if(empty($paramName)){
        //Code here
    }
}
查看更多
贼婆χ
6楼-- · 2020-03-13 06:30

With a single line, you can acomplish it: myHappyFunction($someBogusVar="");

I hope this is what you are looking for. If you read the php documentation, under default argument values, you can see that assigning a default value to an function's argument helps you prevent an error message when using functions.

In this example you can see the difference of using a default argument and it's advantages:

PHP code:

<?php
function test1($argument)
{
    echo $argument;
    echo "\n";
}

function test2($argument="")
{
    echo $argument;
    echo "\n";
}

test1();
test1("Hello");
test1($argument);
$argument = "Hello world";
test1($argument);

test2();
test2("Hello");
test2($argument);
$argument = "Hello world";
test2($argument);
?>

Output for test1() lines:

Warning: Missing argument 1 for test1() .
Hello.
.
Hello world.

Output for test2() lines:

.
Hello.

Hello world.

This can also be used in combination to isset() and other functions to accomplish what you want.

查看更多
▲ chillily
7楼-- · 2020-03-13 06:31

No, because this isn't really anything to do with the function; the error is coming from attempting to de-reference a non-existent array key. You can change the warning level of your PHP setup to surpress these errors, but you're better off just not doing this.

Having said that, you could do something like

function safeLookup($array, $key)
{
  if (isset($array, $key))
    return $array[$key];

  return 0;
}

And use it in place of array key lookup

defaultValue(safeLookup($foo, "bar"), "baz);

Now I need to take a shower :)

查看更多
登录 后发表回答