[英]Fatal error: Uncaught Error: Call to a member function on bool
I know this answer has been answered already but it seems like I couldn't understand anything in my case (I'm still very new to web developing, learning frontend since this october and I jumped onto php at the start of this month).我知道这个答案已经得到了回答,但似乎我无法理解我的情况(我对 web 的开发仍然很陌生,从今年 10 月开始学习前端,我在本月初跳到 php 上)。 My function is supposed to check if user is logged in and I couldn't understand the answers I read for my problem cause it seems like in the answers I found, the functions weren't custom?
我的 function 应该检查用户是否已登录,但我无法理解我为我的问题阅读的答案,因为似乎在我找到的答案中,功能不是自定义的? I might be wrong and I hope if I am, you can laugh about it.
我可能是错的,我希望如果我错了,你可以笑一笑。
So, to get a bit of context, I encounter this error when I try to call the function "check_logged_in()" in one of my controllers, this controller in question (it's called Upload) extends the main Controller from my core folder (and I triple checked if my init file has required it so I can use it globally). So, to get a bit of context, I encounter this error when I try to call the function "check_logged_in()" in one of my controllers, this controller in question (it's called Upload) extends the main Controller from my core folder (and我三次检查了我的初始化文件是否需要它,以便我可以全局使用它)。 Then I call a custom function from the main controller to load models if needed (in this case, I need to load my user model to get access to the "check_logged_in()" function since it's written there).
Then I call a custom function from the main controller to load models if needed (in this case, I need to load my user model to get access to the "check_logged_in()" function since it's written there). And this is where thing happen.
这就是事情发生的地方。 I'll provide a bit of code so you guys can understand what I'm saying.
我将提供一些代码,以便你们理解我在说什么。
The Upload controller上传controller
<?php
class Upload extends Controller
{
function index()
{
header("location:" . ROOT . "upload/image");
die;
}
function image()
{
$user = $this->loadModel("user");
if(!$result = $user->check_logged_in()){
header("location:" . ROOT . "login");
die;
}
$data['page_title'] = "Upload";
$this->view("upload", $data);
}
}
The main Controller主Controller
<?php
class Controller
{
protected function view($view, $data = [])
{
if (file_exists("../app/views/" . $view . ".php")) {
include "../app/views/" . $view . ".php";
} else {
include "../app/views/404.php";
}
}
protected function loadModel($model)
{
if (file_exists("../app/model/" . $model . ".php")) {
include "../app/model/" . $model . ".php";
return $model = new $model();
}
return false;
}
}
And the bit of code from the user model that is called以及来自用户 model 的代码位,称为
<?php
class User
{
function check_logged_in()
{
$DB = new Database();
if (isset($_SESSION['user_url'])) {
$arr['user_url'] = $_SESSION['user_url'];
$query = "select * from users where url_address = :user_url limit 1";
$data = $DB->read($query, $arr);
if (is_array($data)) {
//logged in
$_SESSION['user_name'] = $data[0]->username;
$_SESSION['user_url'] = $data[0]->url_address;
return true;
}
}
return false;
}
}
Thanks in advance for your time mates, I hope my noob self is clear enough for you to understand ^^'提前感谢您的时间伙伴,我希望我的菜鸟自我足够清楚让您理解^^'
Checking for the correct value of the $user variable should fix the error.检查 $user 变量的正确值应该可以修复错误。
...
function image()
{
$user = $this->loadModel("user");
if(!$user || !$user->check_logged_in()){ // Here
header("location:" . ROOT . "login");
die;
}
$data['page_title'] = "Upload";
...
Then on $user
is false
condition will be !false || !false->check_logged_in()
然后在
$user
为false
条件下将是!false || !false->check_logged_in()
!false || !false->check_logged_in()
which will lead to true
. !false || !false->check_logged_in()
这将导致true
。
Otherwise on $user
is User
: !(object User) || !(object User)->check_logged_in()
否则
$user
是User
: !(object User) || !(object User)->check_logged_in()
!(object User) || !(object User)->check_logged_in()
will call User::check_logged_in()
method. !(object User) || !(object User)->check_logged_in()
将调用User::check_logged_in()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.