我想从我的班级
中的db_connect.db访问$ mysqlidb_connect.php
<?php
include_once("db.php");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
mysqli_set_charset($mysqli,"utf8");
?>
class.php
class class{
function __construct() {
include_once 'db_connect.php';
}
function get()
{
if (!$mysqli->connect_errno) {
}
}
}
如果我将db_connect.php直接包含在没有类的php函数中,我可以访问$ mysqli
修改的 我想在类
的所有函数中访问$ mysqli感谢
答案 0 :(得分:2)
您可以将其保存在类变量中:
class class{
private $mysqli;
function __construct() {
include_once 'db_connect.php';
$this->mysqli = $mysqli;
}
function get()
{
if (!$this->mysqli->connect_errno) {
}
}
}