繁体   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