Fatal error: Uncaught Error: Using $this when not in object context [on hold]

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;

        }




    }



?>

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

help me remedy this

Read more here: Fatal error: Uncaught Error: Using $this when not in object context [on hold]

Leave a Reply

Your email address will not be published. Required fields are marked *