致命错误:未捕获的错误:在不在对象上下文中时使用$ this [英] Fatal error: uncaught error: using $this when not in object context

查看:75
本文介绍了致命错误:未捕获的错误:在不在对象上下文中时使用$ this的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个自动化实例化的类,该类如下所示:





I have written a class to automate instantiation , the class looks like this :


<?php

class user{
 protected static $db_table = "users";
 protected static $db_table_fields = array('username ' , 'password' , 'firstname' , 'lastname');
    public $id;
    public $username;
    public $password;
    public $firstname;
    public $lastname;
    //protected static properties = self::get_properties();

    public static function find_user_by_id($id){
        global $database;
        $result_array = array();
  $sql = "SELECT * FROM users WHERE id = '$id'";
        $result = $database->db->query($sql);
        while ($fetch = $result->fetch_array()){

         $result_array[] = self::inst($fetch);

        }
        return array_shift($result_array);
        }




        public static function inst($fetch){

            $instance = new User();
            $var = $this->get_properties();
           // $var = get_object_vars($instance);
            foreach($fetch as $key => $value){

                if(array_key_exists($key , $var)){

                    $instance->$key = $value;

                }

            }

            return $instance;

        }

        public function get_properties(){

           $properties = array();
            $tables = Self::$db_table_fields;
            foreach($tables as $field){

               if(property_exists($this , $field))

               {
                    $properties[] = $this->$field;

               }

            }

            return properties;

        }


    }

?>







我收到致命错误:未捕获错误:在/ var / www中不在对象上下文中时使用$ this /clients/client0/web6/web/admin/admin_includes/user.php:33堆栈跟踪:#0 /var/www/clients/client0/web6/web/admin/admin_includes/user.php(20):user :: inst(Array)#1 /var/www/clients/client0/web6/web/admin/index.php(52):user :: find_user_by_id(17)在#var / www / clients / client0中抛出#2 {main}请拨打第33行的/web6/web/admin/admin_includes/user.php,请帮我解决此问题。



我尝试了什么:



我试图让函数公开但也失败了。




and I get Fatal error: Uncaught Error: Using $this when not in object context in /var/www/clients/client0/web6/web/admin/admin_includes/user.php:33 Stack trace: #0 /var/www/clients/client0/web6/web/admin/admin_includes/user.php(20): user::inst(Array) #1 /var/www/clients/client0/web6/web/admin/index.php(52): user::find_user_by_id(17) #2 {main} thrown in /var/www/clients/client0/web6/web/admin/admin_includes/user.php on line 33 when calling it , please help me remedy this.

What I have tried:

I tried to make functions public but failed as well.

推荐答案

db_table = users;
protected static
db_table = "users"; protected static


db_table_fields = array(' 用户名'' 密码',< span class =code-string>' firstname'' 名字');
public
db_table_fields = array('username ' , 'password' , 'firstname' , 'lastname'); public


id ;
public
id; public


这篇关于致命错误:未捕获的错误:在不在对象上下文中时使用$ this的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆