2012-07-02 68 views
0

一開始我有3個PHP文件來處理3個不同的AJAX查詢。如何加載這個類(靜態/非靜態方法)?

我決定創建一個獨特的PHP類,在那裏我得到$ _POST參數,然後調用3個函數中的1個。

我有另一個PHP類僅用於數據庫查詢(base.php)。我將這個文件包含在我的類中用於獲取AJAX查詢(ajax.php)和我的index.php文件中。

首先,我寫了這樣的事情(ajax.php):

<?php 
    new Ajax(); 

    class Ajax 
    { 
     private $base; 

     public function __construct() 
     { 
      include("base.php"); 
      $this->base = new Base(); 

      $script = $_POST["script"]; 

      $reference = $_POST["reference"]; 

      if($script == "correct" || $script == "insert") { 
       $ptGauche = json_decode($_POST["repGauche"]); 
       $ptDroite = json_decode($_POST["repDroite"]); 
      } 
      if($script == "insert") { 
       $txtGauche = json_decode($_POST["motGauche"]); 
       $txtDroite = json_decode($_POST["motDroite"]); 
      } 

      switch($script) 
      { 
       case "correct": 
        $this->correct($reference, $ptGauche, $ptDroite); 
        break; 
       case "insert": 
        $this->insert($reference, $ptGauche, $ptDroite, $txtGauche, $txtDroite); 
        break; 
       case "delete": 
        $this->delete($reference); 
        break; 
      } 
     } 

     private function correct($reference, $ptGauche, $ptDroite) 
     { 
      // code 
      $query_result = $this->base->selectAllQuery($reference); 
     } 

     private function insert($reference, $ptGauche, $ptDroite, $txtGauche, $txtDroite) 
     { 
      //code 
      $this->base->insertQuery($reference, $txtGauche, $txtDroite, $new_ptGauche, $new_ptDroite); 
     } 

     private function delete($reference) 
     { 
      //code 
      $this->base->deleteQuery($reference); 
     } 
    } 
?> 

顯然,這不是一個好主意,做new Ajax();和不保存類的實例變量中像$ajax = new Ajax();Class instance without variable 有人說這是不是標準的在類構造函數__construct中放置帶有副作用的代碼,也許我應該使用靜態方法,而不是使用構造函數創建對象,並執行類似Ajax::functionToLoad();的操作。

這對我的腳本來說不起作用,因爲我使用了這個包含的非靜態類,所以我的屬性private $base;應該是非靜態的,並且不可能執行數據庫查詢,因爲「static functions can 「T訪問非靜態屬性」:/ http://www.programmerinterview.com/index.php/c-cplusplus/can-static-function-access-non-static-members-of-class/

所以,我使用的非靜態方法和,而不是使用一個構造函數我這樣做:

$ajax = new Ajax(); 
$ajax->loader(); 

class Ajax 
{ 
    private $base; 

    public function loader() 
    { 
     // code 
    } 

    // correct(), insert() and delete() functions 
} 

它是確定要做到這一點或者我應該以另一種方式來做?

回答

0

公共函數Ajax()....是缺省構造函數,如果__construct不存在。因此,使用Ajax而不是加載器,或者創建一個函數Ajax來執行$ this-> loader();

+0

感謝試圖幫助訪問實例,則不能運行阿賈克斯()與__construct相同?爲什麼我應該使用Ajax()而不是__constuct,如果它們都是構造函數? – baptx

0

什麼你正在尋找被稱爲單身

<?php 
class Ajax 
{ 
    private static $inst = new Ajax(); 

    private $base; 

    public static function getInstance(){ 
     return $inst; 
    } 

    public function __construct() 
    { 
     include("base.php"); 
     $this->base = new Base(); 

     $script = $_POST["script"]; 

     $reference = $_POST["reference"]; 

     if($script == "correct" || $script == "insert") { 
      $ptGauche = json_decode($_POST["repGauche"]); 
      $ptDroite = json_decode($_POST["repDroite"]); 
     } 
     if($script == "insert") { 
      $txtGauche = json_decode($_POST["motGauche"]); 
      $txtDroite = json_decode($_POST["motDroite"]); 
     } 

     switch($script) 
     { 
      case "correct": 
       $this->correct($reference, $ptGauche, $ptDroite); 
       break; 
      case "insert": 
       $this->insert($reference, $ptGauche, $ptDroite, $txtGauche, $txtDroite); 
       break; 
      case "delete": 
       $this->delete($reference); 
       break; 
     } 
    } 

    private function correct($reference, $ptGauche, $ptDroite) 
    { 
     // code 
     $query_result = $this->base->selectAllQuery($reference); 
    } 

    private function insert($reference, $ptGauche, $ptDroite, $txtGauche, $txtDroite) 
    { 
     //code 
     $this->base->insertQuery($reference, $txtGauche, $txtDroite, $new_ptGauche, $new_ptDroite); 
    } 

    private function delete($reference) 
    { 
     //code 
     $this->base->deleteQuery($reference); 
    } 
} 
?> 

您現在應該能夠如下

<?php 
     $defaultInst = Ajax::getInstance(); 
?> 
+0

謝謝,也許我沒有精確它,但它只是一個用於AJAX查詢的類,所以我不需要在類之外使用類實例,此處爲$ defaultInst,類 – baptx