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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵