0

我有一个快速的问题。我一直在开发和设计一个面向对象的站点,但是到目前为止,整个过程的一部分还不是面向对象的。那是处理表单的脚本。

我已经四处寻找并没有找到答案,所以我不完全确定是否可能,希望你们能够为我澄清。

我的表单发布到一个标准脚本,例如 login.php,我希望我的表单发布到一个类,或者更准确地说,该类中的一个方法。这是可能吗?

非常感谢,杰

4

2 回答 2

4

您不能直接发布到一个类,但您可以将参数发送到该类中的函数,例如:

$class = new Forms();
$class->processForm($_POST, 'login');

// it would call `processForm()` from `Forms` class and send `$_POST` and `login` as parameters 

或者您可以使用以下__construct()功能:

class Forms {

    function __construct($array, $type){
        switch($type){
            case 'login':
                $this->processLogin($array);
                break;
            //......
        }
    }

    function processLogin($array){
        // do something with $array;
    }

}

$class = new Forms($_POST, 'login');
于 2012-10-05T12:32:00.510 回答
0

您可以使用像这样的基类,它在构造函数阶段自动处理自身并继承具有指定字段的特定表单。不要忘记一些渲染代码。(此处未显示,因为它是系统特定的。)

class Form {

    protected $name = "generic_form";
    protected $method = "POST";
    protected $url = "";

    protected $fields = array();
    protected $data = array();
    protected $submits = array();
    protected $errors = array();
    protected $required = null;
    protected $submitsrequired = array();


    public function __construct() {
        $this->FillSubmits();
        $this->FillData(); 
        foreach($this->fields as $key => $value) {
            if(isset($this->data[$value["name"]])) {
                $this->fields[$key]["value"] = $this->data[$value["name"]];
            }
        }
        $action = null;
        foreach($this->submits as $key => $value) {
            if (isset($this->data[$value["name"]])) {
                $action = $value["name"];
                break;
            }

        }
        if ($action === null) {
            return;
        }
        if (!$this->innerValidate($action)) {
            return;
        }
        $this->Commit($action);
    }

    protected function Validate($action, $name, $value) {
      // Put validation code here
    }

    protected function Commit($action) {
      // Put code to execute when form is correctly filled here
    }

    protected function FillSubmits() {
        foreach($this->fields as $field) {
            if($field["type"] == "submit")
                $this->submits[] = $field;
        }

    }

    protected function FillData() {
        foreach($this->fields as $field) {
            if(isset($_POST[$field["transmitname"]]))
                $this->data[$field["name"]] = $_POST[$field["transmitname"]];
        }
    }

    protected function innerValidate($action) {
        $retval = true;
        if ($this->required !== null) {
            foreach($this->required as $value) {
                if (!isset($this->data[$value])) {
                    return false;
                }
            }
        }
        if (isset($this->submitsrequired[$action])) {
            foreach($this->submitsrequired[$action] as $value) {
                if (!isset($this->data[$value])) {
                    return false;
                }
            }
        }
        foreach($this->data as $key => $value) {
            if(is_callable(array($this, "Validate_".$key))) {
                $retval &= call_user_func(array($this, "Validate_".$key),$action, $value);
            }
            else {
                $retval &= $this->Validate($action, $key, $value);
            }
        }
        return $retval;
    }



}
于 2012-10-05T12:45:42.163 回答