簡體   English   中英

如何加載此類(靜態/非靜態方法)?

[英]How to load this class (static/non-static method)?

最初,我有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();不是一個好主意new Ajax(); 而不是將類實例保存在$ajax = new Ajax();等變量中$ajax = new Ajax(); 沒有變量的類實例有人說將帶有副作用的代碼放在類構造函數__construct是不標准的,也許我應該使用靜態方法而不是使用構造函數創建對象並執行類似Ajax::functionToLoad();

這對我的腳本不起作用,因為我使用的是此類中包含的非靜態類,因此我的屬性private $base; 應該是非靜態的,並且將無法執行數據庫查詢,因為“靜態函數無法訪問非靜態屬性”:/ http://www.programmerinterview.com/index.php/c-cplusplus/can-static功能訪問非靜態成員類/

因此,我正在使用非靜態方法,而不是使用構造函數,而是這樣做的:

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

class Ajax
{
    private $base;

    public function loader()
    {
        // code
    }

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

可以這樣做還是應該以其他方式進行?

如果__construct不存在,則公共函數Ajax()....是默認的構造函數。 因此,請使用Ajax而不是loader或創建一個執行$ this-> loader();的函數Ajax。

您正在尋找的被稱為單例

 <?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();
 ?> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM