Php function argument error suppression, empty() isset() emulation

后端 未结 14 527
执笔经年
执笔经年 2021-01-11 17:32

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 exi

相关标签:
14条回答
  • 2021-01-11 18:17

    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
        }
    }
    
    0 讨论(0)
  • 2021-01-11 18:19

    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.

    0 讨论(0)
  • 2021-01-11 18:19

    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.

    0 讨论(0)
  • 2021-01-11 18:25

    @Sean That was already answered by Brian

    return isset($input) ? $input : $default;
    
    0 讨论(0)
  • 2021-01-11 18:25

    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.

    0 讨论(0)
  • 2021-01-11 18:25

    While the answer to the original question is "no", there is an options no one has mentioned.

    When you use the @ sign, all PHP is doing is overriding the error_reporting level and temporarily setting it to zero. You can use "ini_restore('error_reporting');" to set it back to whatever it was before the @ was used.

    This was useful to me in the situation where I wanted to write a convenience function to check and see if a variable was set, and had some other properties as well, otherwise, return a default value. But, sending an unset variable through caused a PHP notice, so I used the @ to suppress that, but then set error_reporting back to the original value inside the function.

    Something like:

    $var = @foo($bar);
    
    function foo($test_var)
    {
         ini_restore('error_reporting');
    
         if(is_set($test_var) && strlen($test_var))
         {
              return $test_var;
         }
         else
         {
              return -1;
         }
    }
    

    So, in the case above, if $bar is not set, I won't get an error when I call foo() with a non-existent variable. However, I will get an error from within the function where I mistakenly typed is_set instead of isset.

    This could be a useful option covering what the original question was asking in spirit, if not in actual fact.

    0 讨论(0)
提交回复
热议问题