【发布时间】:2019-04-20 17:53:23
【问题描述】:
我有一种情况,在我的 PHP 文件顶部位置创建的对象在到达该文件的 PHP 代码末尾之前被解构
File1.php:
//* This is all in the same PHP file *//
<?php
session_start();
include 'includes/user.inc.php';
$userOBJ = new User; //Declaring the object
?>
//*** jQuery and HTML code here ***//
<?php
if($userOBJ->isAdmin($_SESSION['session_u-name']) == true){
AdminControl();
}
// This code gets done without any problem
function AdminControl(){
echo "<a id='dbControlAdmin' onclick='changeDisplayAdm()'>Database
Control</a>";
}
?>
//*** More HTML and jQuery Code here ***//
<?php
$userOBJ->getUsersName(); //The object is no longer available when reaching this code
?>
在对象类中,我有这个函数作为解构函数:
public function __destruct(){
echo "<b style='color: red;'>Status:</b> Database Connection->Disconnect";
}
这是通过 Object 调用的函数:
public function isAdmin($user){
$userToGet = $user;
$stmt = $this->Connect()->prepare("SELECT admin_db FROM user_secure WHERE username_db=?");
$stmt->execute([$userToGet]);
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$value = $row["admin_db"];
if($value == 1){
return true;
} else {
return false;
}
}
}
public function getUsersName(){
$stmt = $this->Connect()->prepare("SELECT username_db FROM user_secure");
$stmt->execute();
while($row = $stmt->fetchAll(PDO::FETCH_ASSOC)){
echo "<option value='" . $row['username_db'] . "'>" . $row['username_db'] . "</option>";
}
}
}
当我运行网站时,左上角会出现此消息
状态:数据库连接->断开连接
该对象在__destruct函数中的指令是什么,并且网站部署了一个PHP错误,其中最后一个函数调用是:$userOBJ->getUsersName();
错误是:
注意:未定义索引:C:\wamp64\www\NewKali\includes\user.inc.php 中的 username_db 位于第 57 行
我不知道 __destruct 函数的调用位置或调用它的原因!希望你能帮助我。
感谢您的宝贵时间!
【问题讨论】: