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.

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

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图