doudaochu1699 2011-09-16 16:00
浏览 53
已采纳

OOP PHP - get_object_vars

I saw how get_object_vars works at php.net.

The thing is that I can't make it work in my OOP script.

There's user.php file that extends 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;



}

?>

and here's databaseobject.php itself:

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

The create() function should take the values stored inside the public vars in users.php and store them in DB. now as the values of my vars I'm getting the attributes themselves as for $username I have value 'username'.

Where am I wrong? I'm newbie to OOP PHP... :P

  • 写回答

1条回答 默认 最新

  • dongsang6899 2011-09-16 16:25
    关注

    The problem lies in your query:

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

    Note that you are using array_keys() as both your field names and your values. You should use array_values() for your values instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了
  • ¥15 如何删除这个虚拟音频
  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?