OOP PHP - get_object_vars

时间:2011-09-16 16:00:46

标签: php oop

我看到了get_object_vars在php.net上是如何工作的。

问题是我无法在我的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;
        }
    }
}

create()函数应该将存储在users.php中的公共变量中的值存储在DB中。现在作为我的vars的值,我得到属性本身和$ username我有值'用户名'。

我哪里错了?我是OOP PHP的新手......:P

1 个答案:

答案 0 :(得分:1)

问题在于您的查询:

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

请注意,您使用array_keys()字段名称作为您的值。您应该使用array_values()作为您的值。