0

最初は、3つの異なるAJAXクエリを処理するための3つのPHPファイルがありました。

$ _POSTパラメーターを取得する一意のPHPクラスを作成してから、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(); __constructAjax::functionToLoad();

これは、このクラスに含まれる非静的クラスを使用しているため、スクリプトでは機能しません。したがって、プロパティprivate $base;は非静的である必要があり、「静的関数は非静的関数にアクセスできないため、データベースクエリを実行できません。静的プロパティ」:/ 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
}

これを行っても大丈夫ですか、それとも別の方法で行う必要がありますか?

4

2 に答える 2

0

public function Ajax().... __construct が存在しない場合のデフォルトのコンストラクターです。したがって、ローダーの代わりに Ajax を使用するか、$this->loader(); を実行する関数 Ajax を作成します。

于 2012-07-02T13:00:11.390 に答える
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();
 ?> 
于 2012-07-02T13:00:14.970 に答える