gpt4 book ai didi

php - 从同一类中的函数调用 PHP 类中的函数

转载 作者:可可西里 更新时间:2023-11-01 00:10:21 26 4
gpt4 key购买 nike

我下面有一些代码无法运行,因为我不知道如何调用与另一个函数位于同一类中的函数。我已经尝试使用 $this 但它给了我一个 PHP 错误 Using $this when not in object context...Line 25。我不知道如何解决这个问题,我希望其他人可以给我一些提示,告诉我该怎么做。我的代码在下面,谢谢:)

class SESSION {
function start() {
session_start();
}
function check() {
if ($_SESSION["username"]) {
return $_SESSION["username"];
} else {
return "nli"; //Not logged in :(
}
}
function set_session($username) {
$_SESSION["username"] = $username;
}
function login($username, $password) {
$database = DB::connect();
$passwordsha = sha1($password);
$query = "";
$res = $database->query($query);
$num = $res->num_rows;
if ($num == 0) {
header("Location: login.php?e=1");
} elseif ($num == 1) {
$this->set_session($username);
header("Location: admin.php");
} else {
header("Location: login.php?e=2");
}
}
}

最佳答案

And no, I'm not creating the object

这就是调用 $this->set_session($username); 失败的原因,如果您想遵循与现有代码模式相同的代码模式,您可以这样做:

if ($num == 0) {
header("Location: login.php?e=1");
} elseif ($num == 1) {
self::set_session($username);
header("Location: admin.php");
}

关于php - 从同一类中的函数调用 PHP 类中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9712359/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com