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?