【发布时间】:2011-04-09 05:06:13
【问题描述】:
以下是做什么的?
public static function find_by_sql($sql="")
{
global $database;
$result_set = $database->query($sql);
$object_array = array();
while ($row = $database->fetch_array($result_set))
{
// fetch_array=mysql_fetch_array
$object_array[] = self::instantiate($row);
}
return $object_array;
}
private static function instantiate($record)
{
$object = new self;
foreach($record as $attribute=>$value)
{
if($object->has_attribute($attribute))
{
$object->$attribute = $value;
}
}
return $object;
}
private function has_attribute($attribute)
{
return array_key_exists($attribute, $this->attributes());
}
protected function attributes()
{
// return an array of attribute names and their values
$attributes = array();
foreach(self::$db_fields as $field)
{
if(property_exists($this, $field))
{
$attributes[$field] = $this->$field;
}
}
return $attributes;
}
类属性是,
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
public $power;
所以 while 循环遍历每一行,但 instantiate 函数有什么作用呢? instantiate 是如何优化脚本的?
另外,$object = new self 是什么意思,这是类的新实例吗?当您返回$object 时,您返回的是什么?
谢谢,丹尼尔。
【问题讨论】: