以下內容做了什麼?php幫助 - 實例化函數
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
你回來了什麼?
謝謝Daniel。
林達粉絲,哈?我到 – youngcouple10 2012-08-24 13:17:19