<?php 

class Db_object{


//  public function create(){
//        global $database;
    
//      $sql = "INSERT INTO ".static::$db_table." (username,password,first_name,last_name)";
//      $sql .= "VALUES('";
//      $sql .= $database->escape_string($this->username) . "','";
//      $sql .= $database->escape_string($this->password) . "','";
//      $sql .= $database->escape_string($this->first_name) . "','";
//      $sql .= $database->escape_string($this->last_name) . "')";
 
//      $database->query($sql);
 
//  }
 
//  public function update(){
//     global $database;
//     $sql = "UPDATE ".static::$db_table." SET username='$this->username',password='$this->password',
//     first_name='$this->first_name',last_name='$this->last_name' WHERE id='$this->id'";
//     $database->query($sql);
//     }
 
    public static function find_all_table(){
       return static::find_this_query("SELECT * FROM ".static::$db_table." ");
    }
   
    public static function find_by_id($id){
       global $database;
       $the_result_array = static::find_this_query("SELECT * FROM ".static::$db_table." WHERE ID=$id");
       
       return !empty($the_result_array)? array_shift($the_result_array):false;
    }
 
        //UNIVERSAL QUERY
        public static function find_this_query($sql){
          global $database;
          $result_query = $database->query($sql);
          $the_object_array = array();
          while($row = $database->fetch_assoc($result_query)){
        
           $the_object_array[] = static::instatiation($row);
          }
          return $the_object_array;
        
        }
    
        public static function instatiation($the_record){

         $calling_class = get_called_class();
            $the_object = new $calling_class;
        
        foreach($the_record as $the_attribute => $value){
        
        if($the_object->has_the_attribute($the_attribute)){
        
           $the_object->$the_attribute = $value;
        }
        }
         return $the_object;
        
        }
 
        private function has_the_attribute($the_attribute){
     
          $object_properities = get_object_vars($this);
          return array_key_exists($the_attribute,$object_properities);
       }

       public function properties(){
         $properties = array();
         foreach(static::$db_table_fields as $db_fields){
            if(property_exists($this,$db_fields)){
               $properties[$db_fields] = $this->$db_fields;
            }
         }
         return $properties;
      }
 
       public function create_implode(){
          global $database;
         
          $data_table_db = $this->properties();
       
        $sql = "INSERT INTO ".static::$db_table." (".implode(",", array_keys($data_table_db)).")";
        $sql .= "VALUES('".implode("','", array_values($data_table_db))."')";
       
        $database->query($sql);
        
       
       }
       public function update_implode(){
          global $database;
    
          $data_table_db = $this->properties();
          $data_table_rows = array();
    
          foreach($data_table_db as $key => $value){
             $data_table_rows[] = "{$key}='{$value}'";
    
          }
          $sql = "UPDATE ".static::$db_table." SET ";
          $sql .= implode(",", $data_table_rows);
          $sql .=" WHERE id ='$this->id'";
          $database->query($sql);
         
          }
  
          public function delete_implode($id){
              global $database;
              $sql = "DELETE FROM ".static::$db_table." WHERE id=$id";
              $database->query($sql);
           }
 

}

$db_object = new Db_object();


?>