use*_*199 2 php oop form-processing
我创建了一个表单元素类.这堂课能够
我试图让它尽可能通用,以使其可重复使用,但因为我是一个关于OOP的新手,如果有人可以检查这个并让我知道如果这是好的OOP也是非常酷的,如果这是对于我想要实现的目标,这是一个很好的解决方案
这是class_FormControl()的基本部分
class FormControl{
var $finalcontrol;
var $class = "form-control";
var $form_error;
protected function StartFormatting($name, $label){
if (isset($_POST[$name]) AND $_POST[$name] != "") {
return false;
}
$this->finalcontrol = "<label for='$name' >$label</label>";
return true;
}
public function get_control(){
return $this->finalcontrol;
}
}
class TextBox extends FormControl{
public function CreateControl($obj, $name, $label, $placeholder, $value = ""){
if($this->StartFormatting($name, $label)){
$this->finalcontrol .= "<input type='text' class='$this->class' id='$name' name='$name' placeholder='$placeholder'";
if ($value != "") {
$this->finalcontrol .= " value='$value' ";
}
$this->finalcontrol .= ">";
return true;
}
$func = "set_" . $name;
$obj->$func($_POST[$name]);
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我在表单页面中使用该类的方法:
$r1 = New Recipe();
$tbx = new TextBox();
$ctrl1 = $tbx->CreateControl($r1, "Name", "Nombre", "Nombre", $r1->get_Name());
Run Code Online (Sandbox Code Playgroud)
现在,如果$ ctrl1为真,我继续将对象保存在数据库中.
如果$ ctrl1为假,我继续
echo $tbx->get_control();
Run Code Online (Sandbox Code Playgroud)
在页面的正确位置.
/谢谢!
注意:这不是唯一的方法; 这是众多的一个,这是我在午休时间10分钟内对问题的个人解释.请记住,这将是我的实施,我得到的信息很少.在现实世界中,我会发现更多关于域名的信息,但我仍然坚持我的对象中的单一责任原则,并保持一切松散耦合.这些是从这篇文章中拿走的主要内容.
首先,你需要从对象的角度思考.每个对象都有自己的单一责任 维基百科条目:
在面向对象的程序设计中,单一责任原则规定每个上下文(类,函数,变量等)应该只有一个责任,并且该责任应该完全由上下文封装.其所有服务应与该责任严格一致.[强调我自己].
你正在做的是将程序代码放入类方法中.这不会使它面向对象!你需要改变你的心态!
您正在构建表单构建器.表单由元素组成.我立刻想到:
Form
对象FormElement
对象上述对象是Entity或ValueObject的特定表示.领域驱动设计.
FormElement
可以是一个接口,所有表单元素(如输入框,按钮等)必须符合.
class Form
{
/**
* @var FormElement[]
*/
protected $elements;
/**
* Add a FormElement to the form
*
* @param FormElement $element
*/
public function addFormElement(FormElement $element)
{
$this->elements[] = $element;
}
}
interface FormElement
{
/**
* @return The generated html for the given form element
*/
public function getHtml();
}
Run Code Online (Sandbox Code Playgroud)
现在,所有你需要做的就是确保每个对象实现的FormElement
返回你想要的内到底是什么FormElement::getHtml()
方法,当你添加新的元素到它会所有的工作仍然很大Form
,因为这将是Form
正在调用getHtml()
的在将每个FormElement
对象添加到它自己的HTML并输出之前循环它们.
这是TextBoxFormElement
我要使用的一个例子:
class TextBoxFormElement implements FormElement
{
/**
* @constructor
*
* This is where I am declaring that, for this to be a VALID object, it MUST have
* the following properties passed in
*
* @param string $name
* @param string $label
* @param string $placeholder
* @param string $value
*/
public function __construct($id, $class, $name, $label, $placeholder, $value)
{
$this->id = $id;
$this->class = $class;
$this->name = $name;
$this->label = $label;
$this->placeholder = $placeholder;
$this->value = $value;
}
/**
* Generate the html of this element
*
* @return string
*/
public function getHtml()
{
return sprintf(
"<input type='text' id='%s' class='%s' name='%s' label='%s' placeholder='%s' value='%s'>",
$this->id, $this->class, $this->name, $this->label, $this->placeholder, $this->value
);
}
}
Run Code Online (Sandbox Code Playgroud)
您正在使用$_POST
,并$_GET
在这些类.你不应该这样做.你应该做以下事情:
您编写的每个对象,这些对象的公共方法都决定了它们的API.您正在为这些对象的用法编写API.[我自己的引用]
实际上,有任何形式的$_POST
或$_GET
夫妻这些对象,这些超全局变量的状态.你打算怎么测试它们?每次你想要测试它们时,你将不得不模拟(或伪造)这些超全球的内容.
不.你应该做的是暂时忘记你自己的应用程序并对这些对象进行编码,这样你就可以在任何应用程序中使用它们.然后,将值传递给对象构造函数或方法.不要在本课程中使用$_POST
或$_GET
直接使用.
你不是在写OO代码.但我不打算为你写.但是,我将编写我希望在使用您的库时调用的方法,希望您能够自己弄清楚自己的实现.保持一切分开,对每个对象使用单一责任(这不是过度杀伤,它是面向对象的编程),并继续学习:
// Sanitize and validate input data obviously you would have a RequestValidator used before you even get to this point, making sure exactly what you want can be used by the library
$name = $_POST['name'];
/** Create our form **/
$form = new Form;
/** TextBoxFormElement implements FormElement **/
$textBox1 = new TextBoxFormElement($name, 'Label', 'Placeholder' /** etc **/);
/** Add our FormElement **/
$form->addFormElement($textBox1);
/** Generate our html - the form generates it's own HTML as well as calling getHTML() on the FormElement objects in a loop **/
echo $form->getHtml();
Run Code Online (Sandbox Code Playgroud)
那就是IT.这是您应该创建的对象API.它应该就这么简单.现在开始学习这些神奇的知识并学习.
额外信息:另一种方法是查看访客模式.基本上,您将生成输出的东西与保存数据的东西分开.如果你想看一下这里有关于这个主题的更多信息.
永远记住,您的代码应该易读且易于理解,如果有帮助,您应该创建一些行业标准的UML图表以与代码一起使用.