2012-10-05 91 views
0

我有一個簡短的問題。我一直在開發和設計一個面向對象的站點,但目前整個過程的一部分不是OOP。這是處理表單的腳本。從一個類處理表單,php

我周圍搜索,還沒有找到答案,所以我不完全確定是否有可能,希望你們能夠爲我澄清。

我的表單發佈到一個標準的腳本,比如login.php,我希望我的表單發佈到一個類,或者更準確地說,是該類中的一個方法。這是可能嗎?

非常感謝, 周杰倫

+0

什麼你正試圖在這裏發明了叫做MVC,你的形式表示的控制器,以及你的方法是一種行動 – JvdBerg

回答

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'); 
0

您可以使用基類的這樣一個過程中構造階段autoprocesses本身和繼承具體FO有指定字段的rms。不要忘記一些渲染代碼。 (這裏沒有顯示出來,因爲它是系統的特定性很高。)

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; 
    } 



}