PHP - best way to initialize an object with a large number of parameters and default values

后端 未结 6 1105
旧巷少年郎
旧巷少年郎 2021-01-31 09:20

I\'m designing a class that defines a highly complex object with a ton (50+) of mostly optional parameters, many of which would have defaults (eg: $type = \'foo\'; $width

6条回答
  •  借酒劲吻你
    2021-01-31 09:59

    You also could make a parent class.

    In that class you only define the variables.

    protected function _SetVarName( $arg ){
    
       $this->varName=$arg;
    }
    

    Then extend that class into a new file and in that file you create all your processes.

    So you get

    classname.vars.php
    classname.php
    
    classname extends classnameVars {
    
    }
    

    Because most will be on default you only have to Set/Reset the ones you need.

    $cn=new classname();
    $cn->setVar($arg);    
    //do your functions..
    

提交回复
热议问题