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

后端 未结 14 690
执笔经年
执笔经年 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:15

    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.

提交回复
热议问题