doulun1915 2016-07-08 08:15
浏览 48
已采纳

OOP和switch语句

I have this OOP code in php

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);

What to do about the switch statement?Later I may add textarea input and I have to go and edit the switch statemt.I have looked here https://sourcemaking.com/design_patterns but I don't know with one fits this case maybe factory no idea.This is my first OOP try. By the way the array $test1 must not be changed I mean the way some one uses those clases must be the same.Any help really appreciated.Thank you. Edit:The question is:Is anything wrong if I use the switch statement?Is a better way to do this?

  • 写回答

1条回答 默认 最新

  • dongtang6775 2016-07-12 01:46
    关注

    You could create class map, and special methods to create inputs from options.

    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);
    

    Also, you could add options validator to make sure that all mandatory options has passed and there is no extra options.

    Why not ucfirst? Because you are able to use camel case class name, for example RichText (textarea with wysiwyg). Or write more smart class recognizer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常