我想从类中的db_connect.db访问$mysqli
db_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) {
}
}
}
如果我在没有类的php函数中直接包含db_connect.php,我可以访问$mysqli
编辑我想在类的所有函数中访问$mysqli
感谢
您可以将其保存在一个类变量中:
class class{
private $mysqli;
function __construct() {
include_once 'db_connect.php';
$this->mysqli = $mysqli;
}
function get()
{
if (!$this->mysqli->connect_errno) {
}
}
}