<?php
class entity_site
{
private $SiteID;
private $SiteName;
private $SiteUrl;
private $SiteContactInfo;
private $SiteAudit;
private $SiteElite;
private $SiteClassID;
public function __get($PropertyName)
{
return($this->$PropertyName);
}
public function __set($PropertyName, $Value)
{
$this->$PropertyName = $Value;
}
}
?>
那么假设我现在把从数据库中获取的记录集放在数据$arr里面,我应该怎么实现?
我的思路是:
遍历记录集
{
遍历字段
{
如果(实体属性中有和字段名相等的))
{
实体的属性 = $row[$field](这条记录的字段的值)。
}
}
}
请赐教。具体语法不知道怎么写,谢谢!
private $DeptID;
private $DeptName;
private $DeptNameEn;
private $IsBiz;
private $IsGZ;
private $FirstChar;
private $IsDel;
function __construct($DeptID,$DeptName,$DeptNameEn,$IsBiz,$IsGZ,$FirstChar,$IsDel){
$this->DeptID=$DeptID;
$this->DeptName=$DeptName;
$this->DeptNameEn=$DeptNameEn;
$this->IsBiz=$IsBiz;
$this->IsGZ=$IsGZ;
$this->FirstChar=$FirstChar;
$this->IsDel=$IsDel;
}
function __get($var){
return $this->$var;
}
}这是类似的结果集;之后循环,实例化那个实体类
if(count($DeptMains)>0){
foreach ($DeptMains as $DeptMain){
$Dept[]=new DeptMain($DeptMain['DeptID'],$DeptMain['DeptName'],$DeptMain['DeptNameEn'],$DeptMain['IsBiz'],$DeptMain['IsGZ'],$DeptMain['FirstChar'],$DeptMain['IsDel']);
}
$resArr = array();
$resArr['name'] = $rows['name'];//假设有这个name字段
$resArr['age'] = $rows['age'];
class中加
var $name;
var $age;
function __construect($name,$age){
$this->name = $name;
$this->age = $age;
}
......
$obj = new example($resArr['name'],$resArr['age']);
将记录集放入list里面。
http://www.php.net/manual/en/function.mysql-fetch-array.php