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

后端 未结 6 1202
旧巷少年郎
旧巷少年郎 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:52

    Just a little improvement on Daff's first solution to support object properties that may have a null default value and would return FALSE to the isset() condition:

     $val) {
                $name = '_' . $key;
                if(property_exists(get_called_class(),$name))
                    $this->{$name} = $val;
                }
            }
        }
    }
    
    ?>
    

提交回复
热议问题