2011-09-16 57 views
1

我看到了get_object_vars如何在php.net中工作。OOP PHP - get_object_vars

事情是,我不能讓它在我的OOP腳本中工作。

有擴展DatabaseObject user.php的文件:

<?php 
require_once('database.php'); 

class User extends DatabaseObject { 

    protected static $tableName = 'users'; 
    protected static $tableID = 'id'; 

    public $id; 
    public $username; 
    public $password; 
    public $firstname; 
    public $lastname; 



} 

?> 

和這裏的databaseobject.php本身:

<?php 
require_once('database.php'); 

class DatabaseObject { 


    public static function findAll(){ 
     global $database; 
     $calledClass = get_called_class();  
     return self::findBySQL("SELECT * FROM ".static::$tableName.""); 
    } 

    public static function findByID($id){ 
     global $database; 
     $calledClass = get_called_class();  
     $result_array = self::findBySQL("SELECT * FROM ".static::$tableName." WHERE ".static::$tableID." = {$id}"); 
     return !empty($result_array) ? array_shift($result_array) : false; 
    } 

    public static function findBySQL($sql){ 
     global $database; 
     $result_set = $database->query($sql); 
     $object_array = array(); 
     while ($row = $database->fetchArray($result_set)) { 
      $object_array[] = self::instantiate($row); 
     } 
     return $object_array;  
    } 

    private static function instantiate($record){ 
     $calledClass = get_called_class();  
     $object = new $calledClass; 
     foreach($record as $attribute=>$value){ 
      if($object->has_attribute($attribute)) { 
      $object->$attribute = $value; 
      } 
     } 
     return $object; 
    } 

    private function has_attribute($attribute) { 
     $object_vars = $this->attributes(); 
     return array_key_exists($attribute, $object_vars); 
    } 

    public function attributes(){ 
     return get_object_vars($this); 
    } 

    protected function cleanAttributes(){ 
     global $database; 
     $cleanAttributes = array(); 
     foreach($this->attributes() as $key => $value) { 
      $cleanAttributes[$key] = $database->escapeValue($value); 
     } 
     return $cleanAttributes; 
    } 

    public function save() { 
     return(isset($this->id)) ? $this->update() : $this->create(); 
    } 

    protected function create() { 
     global $database; 
     //$calledClass = get_called_class(); 
     //$class = new $calledClass; 
     $attributes = $this->cleanAttributes();  
     $sql = "INSERT INTO ".static::$tableName." ("; 
     $sql .= join(", ", array_keys($attributes)); 
     $sql .= ") VALUES ('"; 
     $sql .= join("', '", array_keys($attributes)); 
     $sql .= "')"; 
     if($database->query($sql)) { 
      $this->id = $database->insert_id(); 
      return true; 
     }else { 
      return false; 
     } 
    } 
} 

的創建()函數應存儲的公開瓦爾內的用戶價值.php並將它們存儲在數據庫中。現在作爲我的變量的價值我得到的屬性本身爲$ username我有價值'用戶名'。

我在哪裏錯了?我是新手,OOP PHP ...:P

+0

@xdazz今天你的幫助我設法解決我的問題,並瞭解我錯了什麼地方..也許這次你也可以救我... – mrGott

+1

看起來像可怕的設計,作爲你的其他職位。請不要創建重複項,並瞭解靜態和實例變量。 – Macmade

+1

可怕的問題標題也是。 –

回答

1

問題就出在你的查詢:

$sql = "INSERT INTO ".static::$tableName." ("; 
    $sql .= join(", ", array_keys($attributes)); 
    $sql .= ") VALUES ('"; 
    $sql .= join("', '", array_keys($attributes)); 
    $sql .= "')"; 

請注意,您正在使用array_keys()既是你的字段名你的價值觀。您應該使用array_values()代替您的值。