OOP 和 switch 语句

OOP and switch statements

我在 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.I 已经看过这里 https://sourcemaking.com/design_patterns 但我不知道是否合适这种情况下工厂编号 idea.This 可能是我的第一次 OOP 尝试。 顺便说一句,数组 $test1 不能更改我的意思是某些人使用这些类的方式必须是 same.Any 真正帮助 appreciated.Thank 你。 Edit:The问题is:Is如果我使用switch语句有什么问题吗?有更好的方法吗?

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

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?因为您可以使用驼峰式 class 名称,例如 RichText(所见即所得的文本区域)。或者编写更智能的 class 识别器。