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?
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.