doutuo1908 2014-07-09 12:17
浏览 15
已采纳

类抽象不起作用

I have a class naming user with the code below

require_once(LIB_PATH.DS.'database.php');
class User extends DatabaseObject {
    protected static $table_name="users";
    public $id;
    public $username;
    public $password;
    public $first_name;
    public $last_name;
 }

i extended this User class from a DatabaseObject class where i had put up common methods for database CRUD at the moment i had just put up a Create method below is my DatabaseObject Class

require_once(LIB_PATH.DS.'database.php');

class DatabaseObject {

   public static function attributes() { 
        $class_name = get_called_class();
        $object = new $class_name;
        return get_object_vars($object);
    }

   public static function sanitized_attributes() {
      global $database;
      $clean_attributes = array();
      foreach(static::attributes() as $key => $value){
         $clean_attributes[$key] = $database->escape_value($value);
      }
   return $clean_attributes;
  }

    public static function create() {
        global $database;
        $attributes = static::sanitized_attributes();
      $sql = "INSERT INTO ".static::$table_name." (";
      $sql .= join(", ", array_keys($attributes));
      $sql .= ") VALUES ('";
      $sql .= join("', '", array_values($attributes));
      $sql .= "')";
      if($database->query($sql)) {
        return $sql;
      } else {
     return false;
      }
   }
}

when i m running a test from my test.php to check whether the Create works or not with this code

 $user = new User();
 $user->username = "johnsmith";
 $user->password = "abcd12345";
 $user->first_name = "John";
 $user->last_name = "Smith";
 echo User::create();

it just returns this query with empty values

   INSERT INTO users 
  (id, username, password, first_name, last_name) VALUES ('', '', '', '', '') 

and when i check phpmyadmin a row is inserted but with empty values whats is the mistake i m making please help

Regards

Tapos

  • 写回答

2条回答 默认 最新

  • douyi1944 2014-07-14 09:08
    关注

    Change Your methods to public remove the static keyword and pass a pointer reference to the create method with code below

        $user = new User();
     $user->username = "johnsmith";
     $user->password = "abcd12345";
     $user->first_name = "John";
     $user->last_name = "Smith";
     echo $user->create($user);
    

    as u pass the object to thye create method the rest of your code will be as below

        public function attributes(&$obj) { 
          return get_object_vars($obj);
         }
    
    public function sanitized_attributes(&$obj) {
     global $database;
      $clean_attributes = array();
    
    foreach($obj->attributes($obj) as $key => $value){
        $clean_attributes[$key] = $database->escape_value($value);
      }
      return $clean_attributes;
    }
    
    
    public function create(&$obj) {
        global $database;
    
        $attributes = $obj->sanitized_attributes($obj);
       $sql = "INSERT INTO ".static::$table_name." (";
        $sql .= join(", ", array_keys($attributes));
      $sql .= ") VALUES ('";
        $sql .= join("', '", array_values($attributes));
        $sql .= "')";
      if($database->query($sql)) {
       } else {
        return false;
      }$obj->sanitized_attributes($obj);
    }
    

    hope this works!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路