OOP和switch语句

时间:2016-07-08 08:15:59

标签: php oop software-design

我在php中有这个OOP代码

class SSE {

static function setSection($opt_name,array $settings){
    var_dump($settings["fields"]);

    foreach ($settings["fields"] as $field){
        self::processField($opt_name,$field);
    }

}

static function processField($opt_name,array $field){

        switch ($field["type"]){
            case "number":
                $number = new Number($field["title"],$field["desc"],$field["id"]);
                echo "<br>$number";
                break;
            case "checkbox":
                $checkbox = new Checkbox($field["title"],$field["desc"],$field["id"],$field["color"]);
                echo "<br>$checkbox";
                break;
        }
}

}

class Input {

protected $title;
protected $desc;
protected $id;

}

class Number extends Input {

//protected $fields = array();

function __toString(){
    return $this->title;
}

public function __construct($title,$desc,$id){
    $this->title = $title;
    $this->desc = $desc;
    $this->id = $id;
}
}

class Checkbox extends Input {

//protected $fields = array();
protected $color;
function __toString(){
    return $this->title;
}

public function __construct($title,$desc,$id,$color){
    $this->title = $title;
    $this->desc = $desc;
    $this->id = $id;
    $this->color = $color;
}
}

$test1 = array(
"title" => "Ssadassa",
"id" => "basic",
"desc" =>"this is a test",
"fields" => array(
    array(
        "title" => "Checkbox input",
        "id" => "ba32132sic",
        "desc" =>"this is a test",
        "type"  => "checkbox",
        "color" => "This is only for checkbox no another input should have this"
    ),
    array(
        "title" => "Number input",
        "id" => "basic",
        "desc" =>"this is a test",
        "type"  => "number"
    )
)

);

SSE::setSection("da",$test1);

如何处理switch语句?后来我可能会添加textarea输入,我必须去编辑交换机statemt。我已经看过这里https://sourcemaking.com/design_patterns但我不知道一个适合这种情况可能是工厂不知道。这是我的第一次OOP尝试。 顺便说一下,数组$ test1一定不能改变,我的意思是某些人使用那些clases的方式必须相同。任何帮助真的很感激。谢谢。 编辑:问题是:如果我使用switch语句有什么问题吗?这是一个更好的方法吗?

1 个答案:

答案 0 :(得分:0)

您可以创建类映射,以及从选项创建输入的特殊方法。

class SSE { // please rename this

    static private $mapClass = ['number' => 'Number', 'checkbox' => 'Checkbox'];

    static function setSection($opt_name, array $settings) {
        // var_dump($settings["fields"]);

        foreach ($settings["fields"] as $field) {
            self::processField($opt_name, $field);
        }
    }

    static function processField($opt_name, array $field) {
        // recognize class from class map
        $class = self::$mapClass[$field["type"]];
        $input = $class::createFromOptions($field);
        echo "<br>$input";
    }

}

class Input {

    protected $title;
    protected $desc;
    protected $id;

}

class Number extends Input {

//protected $fields = array();

    function __toString() {
        return $this->title;
    }

    public function __construct($title, $desc, $id) {
        $this->title = $title;
        $this->desc = $desc;
        $this->id = $id;
    }

    // create object from array
    static public function createFromOptions(array $options) {
        return new self($options["title"], $options["desc"], $options["id"]);
    }

}

class Checkbox extends Input {

//protected $fields = array();
    protected $color;

    function __toString() {
        return $this->title;
    }

    public function __construct($title, $desc, $id, $color) {
        $this->title = $title;
        $this->desc = $desc;
        $this->id = $id;
        $this->color = $color;
    }

    // create object from array
    static public function createFromOptions(array $options) {
        return new self($options["title"], $options["desc"], $options["id"], $options["color"]);
    }

}

$test1 = array(
    "title" => "Ssadassa",
    "id" => "basic",
    "desc" => "this is a test",
    "fields" => array(
        array(
            "title" => "Checkbox input",
            "id" => "ba32132sic",
            "desc" => "this is a test",
            "type" => "checkbox",
            "color" => "This is only for checkbox no another input should have this"
        ),
        array(
            "title" => "Number input",
            "id" => "basic",
            "desc" => "this is a test",
            "type" => "number"
        )
    )
);

SSE::setSection("da", $test1);

此外,您可以添加选项验证程序,以确保所有必需选项都已通过,并且没有其他选项。

为什么不ucfirst?因为您可以使用驼峰案例类名称,例如RichText(带有wysiwyg的textarea)。或者写更多智能类识别器。