What does the following do?
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;
}
The class attributes are,
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
public $power;
So the while loop is looping through each row, but what does the instantiate
function do? and how does instantiate
optimize the script?
Also, what does $object = new self
mean, is this a new instance of the class? and when you return the $object
what are you returning?
Thanks, Daniel.