【发布时间】:2014-06-12 03:05:07
【问题描述】:
我有 2 个类 - 一个扩展 DatabaseMaster 类的用户类。我在 DatabaseMaster 类中有一个 create 方法,如下所示...
public function create() {
$attributes = $this->attributes();
$question_marks = array();
foreach ($attributes as $key => $value) {
$question_marks[] = "?";
}
$place_holder = array_intersect_key($attributes, get_object_vars($this));
$place_holder = array_values($place_holder);
$sql = "INSERT INTO ".static::$table_name." (";
$sql .= join(", ", array_keys($attributes));
$sql .= ") VALUES (";
$sql .= join(", ", array_values($question_marks));
$sql .= ")";
$query = $handler->prepare($sql);
$query->execute($place_holder);
}
这会获取调用它的类的属性,并在函数中循环它们以创建 SQL 字符串。工作正常。
如果我创建一个新用户,例如 vardump 我得到的用户....(请注意,我只是输入了一个可见密码作为 james...其余为 NULL 仅用于说明目的)
object(User)#2 (14) {
["id"]=> NULL ["visible_password"]=> string(5) "James"
["hashed_password"]=> NULL
["temp_hashed_password"]=> NULL
["email"]=> NULL
["first_name"]=> NULL
["last_name"]=> NULL
["position"]=> NULL
["location"]=> NULL
["city"]=> NULL
["country"]=> NULL
["institution"]=> NULL
["interests"]=> NULL
["profile_comment"]=> NULL }
我在 DatabaseMaster 类中也有一个 find_by_id() 方法....
public static function find_by_id($id=0) {
$result_array = static::find_by_sql("SELECT * FROM ".static::$table_name." WHERE id={$id} LIMIT 1");
return !empty($result_array) ? array_shift($result_array) : false;
}
现在,如果我使用这个,id=223 和 vardump 我得到的结果....
object(DatabaseMaster)#4 (14) {
["id"]=> string(3) "301"
["visible_password"]=> string(5) "James"
["hashed_password"]=> NULL
["temp_hashed_password"]=> NULL
["email"]=> NULL
["first_name"]=> NULL
["last_name"]=> NULL
["position"]=> NULL
["location"]=> NULL
["city"]=> NULL
["country"]=> NULL
["institution"]=> NULL
["interests"]=> NULL
["profile_comment"]=> NULL }
我需要这个作为用户对象返回......我可以理解为什么它不是(有点)但是因为它们是 find_by_id 方法中的静态调用,为什么这不是用户对象......
感谢您的意见...
【问题讨论】:
标签: php object methods extends