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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler