Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中,如何从类内部调用mysql数据库函数?_Php_Mysql_Static_Singleton_Database Connection - Fatal编程技术网

在PHP中,如何从类内部调用mysql数据库函数?

在PHP中,如何从类内部调用mysql数据库函数?,php,mysql,static,singleton,database-connection,Php,Mysql,Static,Singleton,Database Connection,这是我挣扎了一段时间的事情,我知道我并不孤单。我正在寻找一种“最佳实践”方法,以便能够在PHP类中使用数据库连接。我刚刚开始阅读静态函数和单例。我现在认为这是一条路,但我想避免在买了一把新锤子之后把所有问题都看成钉子,如果你知道我的意思的话。我知道我不是唯一一个对此提出质疑的人,但我找不到一个好的资源来解释应该如何做 我想我正在寻找的是一个可重用的类,它可以删除或最小化任何全局调用。我知道,出于性能原因,我不想创建一个类的多个副本或创建多个数据库实例 我认为我所做的(我对一些单词的定义有点模糊)

这是我挣扎了一段时间的事情,我知道我并不孤单。我正在寻找一种“最佳实践”方法,以便能够在PHP类中使用数据库连接。我刚刚开始阅读静态函数和单例。我现在认为这是一条路,但我想避免在买了一把新锤子之后把所有问题都看成钉子,如果你知道我的意思的话。我知道我不是唯一一个对此提出质疑的人,但我找不到一个好的资源来解释应该如何做

我想我正在寻找的是一个可重用的类,它可以删除或最小化任何全局调用。我知道,出于性能原因,我不想创建一个类的多个副本或创建多个数据库实例

我认为我所做的(我对一些单词的定义有点模糊)是一个数据库单例

我想要的是一些建议。这就是我“应该”使用数据库类的方式吗?如果是的话,我写的是最好的方式吗

以下是PHP:

<?php
# set all errors and notices on.
error_reporting(E_ALL);
ini_set('display_errors', '1');


# database static class.
class database {

    private static $connection;
    private static $instance;

    private function __construct(){
        # run the connection here
        self::$connection='*dbcon*';
        echo 'connecting...<br />';
    }

    public static function getInstance(){
        # this only runs once, calling the construct once.
        if(!self::$instance){
            self::$instance = new database();
        }
        return self::$instance;     
    }

    private static function sanitise($data){
        # sanitise data before it's sent to the database
        return '~'.$data.'~';
        # sanitisation is flagged by adding tildas to a string.
    }

    public function runquery($query){
        echo 'running query...<br />';
        return self::$connection.' - '.self::sanitise($query);
    }
}

# user class
class myuser {

    function getuser($username){
        # this uses the database class, but shouldn't call a second copy of it.
        # I should be able to re-use the connection string created outside this class.
        # Doing so sould not trigger a reconnection (flagged by echoing 'connecting...')
        echo database::runquery('SELECT * FROM user_tbl WHERE username='.$username);
        # The above line will call everything I need to return a result including sanitisation.
    }
}

# this line would be needed to substantiate an instance of the database.
database::getInstance();
# run two queries on the database to see if they both run from the same connection
echo database::runquery('test query');
echo '<br />';
echo database::runquery('second test query');
echo '<br />';

# substantiate a new user class
$user = new myuser();
# load in the current user.
$user->getuser('mark');
?>

我的输出中只有一个“connecting…”字符串,我可以从任何地方调用数据库查询,而无需调用类似
$this->database=new database()的东西
在我创建的每个类的构造函数中。

欢迎使用SO-在发布之前一定要仔细搜索。关于这个相同的话题,这里有很多例子。
connecting...
running query...
*dbcon* - ~test query~
running query...
*dbcon* - ~second test query~
running query...
*dbcon* - ~SELECT * FROM user_tbl WHERE username=mark~