OOP PHP-获取对象变量

OOP PHP-获取对象变量,php,oop,Php,Oop,我在php.net上看到了get_object_vars是如何工作的 问题是,我不能让它在我的OOP脚本中工作 有一个user.php文件扩展了DatabaseObject: <?php require_once('database.php'); class User extends DatabaseObject { protected static $tableName = 'users'; protected static $tableID = 'id';

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

问题是,我不能让它在我的OOP脚本中工作

有一个user.php文件扩展了DatabaseObject:

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

问题在于您的查询:

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

请注意,您使用的是
array_keys()
作为字段名和值。您应该使用
array\u values()
代替您的值。

问题在于您的查询:

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

请注意,您使用的是
array_keys()
作为字段名和值。您应该使用
array\u values()
来代替您的值。

@xdazz在您的帮助下,今天我成功地解决了我的问题,并理解了我的错误所在。。也许这一次你也能救我…看起来很糟糕的设计,就像你的另一篇文章一样。请不要创建重复项,了解静态变量和实例变量。问题标题也很糟糕。谢谢,你的回答非常乐观。。我问问题是因为我不知道怎么做,这就是为什么我想听听社区的想法…@Mikey:其他人试图告诉我的是答案是:“不要问怎么做,因为你一开始就不应该这样做。”。如果您正在学习OOP,请忘记get_object_vars-如果您需要它,这意味着您正在使用对象作为数组。@xdazz在您的帮助下,今天我成功地解决了我的问题并理解了我的错误所在。。也许这一次你也能救我…看起来很糟糕的设计,就像你的另一篇文章一样。请不要创建重复项,了解静态变量和实例变量。问题标题也很糟糕。谢谢,你的回答非常乐观。。我问问题是因为我不知道怎么做,这就是为什么我想听听社区的想法…@Mikey:其他人试图告诉我的是答案是:“不要问怎么做,因为你一开始就不应该这样做。”。如果您正在学习OOP,那么就忘了get_object_vars吧——如果您需要它,这意味着您无论如何都在使用对象作为数组。