Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a class to automate instantiation , the class looks like this :


PHP
<?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;

        }


    }

?>



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.
Posted
Updated 4-Apr-17 21:20pm

You are using $this within a static class member function:
PHP
public static function inst($fetch){
    $instance = new User();
    $var = $this->get_properties();

You either have to make the function non-static or make the function get_properties() static. But the latter would probably make no sense.

See also PHP: Static Keyword - Manual[^]:
Quote:
Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.
 
Share this answer
 
Quote:
$instance = new User();
$var = $this->get_properties();

Possibly you meant
PHP
$instance = new User();
$var = $instance->get_properties();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900