简体   繁体   English

致命错误:未捕获的错误:调用 bool 上的成员 function

[英]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()然后在$userfalse条件下将是!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()否则$userUser : !(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.

相关问题 致命错误:未捕获错误:在 bool 上调用成员函数 fetch() - Fatal error: Uncaught Error: Call to a member function fetch() on bool 致命错误:未被捕获的错误:在布尔上调用成员函数RowCount() - Fatal error: Uncaught Error: Call to a member function RowCount() on bool 致命错误:未捕获的错误:调用成员函数 - Fatal error: Uncaught Error: Call to a member function 致命错误:未捕获的错误:在第 58 行调用 bool 上的成员函数 format() - Fatal error: Uncaught Error: Call to a member function format() on bool on line 58 致命错误:未捕获错误:调用成员 function fetchAll() on bool php mvc pdo - Fatal error: Uncaught Error: Call to a member function fetchAll() on bool php mvc pdo 当我在 php 中执行查询时,我得到:致命错误:未捕获的错误:调用成员 function close() on bool in - When i execute a query in php, i get: Fatal error: Uncaught Error: Call to a member function close() on bool in “致命错误:未捕获的错误:调用成员 function bind_param() on bool”它说 - "Fatal error: Uncaught Error: Call to a member function bind_param() on bool" it says PHP8 PDO SQLite3 执行 =&gt; fetch* =&gt; 致命错误:未捕获错误:调用成员 function fetch() on bool - PHP8 PDO SQLite3 execute => fetch* => Fatal error: Uncaught Error: Call to a member function fetch() on bool 致命错误:未被捕获的错误:在以下位置,调用成员函数getReference()的null: - Fatal error: Uncaught Error: Call to a member function getReference() on null in 致命错误:未捕获的错误:在 wordpress 中在 null 上调用成员函数 insert() - Fatal error: Uncaught Error: Call to a member function insert() on null in wordpress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM