如何从另一个Php文件访问Php静态变量



我有一个这样的静态变量来跟踪内存中的一些操作,而不使用真正的数据库

<?php
class Database
{
public static $database = array();
}

当我试图从另一个php文件访问数据库时,如

<?php
include 'database.php';
function createPaymentRef($username, $password, $amount)
{
$finalResult = array();
$ref = generateTimedTransactionRef($username, $password, $amount);
global $requestData;
global $ip;
Database::$database->unset($username); //Expected type 'object'. Found 'array'.intelephense(1006) error
$requestData->ip = $ip;
$requestData->reference = $ref;
$finalResult['result'] = $ref;
return $finalResult;
}
?>

我得到一个错误说

Expected type 'object'. Found 'array'.intelephense(1006)

如何解决这个问题?

->用于访问对象的属性或方法。Database::$database是一个数组,而不是对象。取消数组元素设置的语法是

unset($array[$index])

所以应该是

unset(Database:$database[$username]);

检查静态变量是否存在,如果其中存在数组元素,则使用unset(Database::$database[$username]):

取消设置
$db = Database::$database;
if($username && $db && isset($db[$username])){
unset(Database::$database[$username]);
}

最新更新