foreach循环中$p[] = $MaterialType 的疑问
<br />class so_getMaterialType<br />{<br /> var $CreateTime = '';<br /> var $IsDefault = '';<br /> var $UpdateTime = '';<br />}<br /><br /><br />$p = array();<br />$sql = 'SELECT * from tb where lid<50';<br />$re_materialTypeID = $db->getAll($sql);<br /><br />foreach($re_materialTypeID as $key => $val)<br />{<br /> $getMaterialType = new so_getMaterialType ; //为什么每次循环都要新申请一遍,而不是放在循环外面申请一次即可??<br /> <br /> $getMaterialType->CreateTime = $val['CreateTime'];<br /> $getMaterialType->IsDefault = $val['IsDefault'];<br /> $getMaterialType->UpdateTime = $val['UpdateTime'] ;<br /><br /> $p[] = $getMaterialType ;<br />}<br />var_dump($p);<br />return $p;<br /> <br />/*————————这种写法与上面差别是什么??———————————*/<br /><br />$getMaterialType = array();<br /><br />$getMaterialType[] = new so_getMaterialType ;<br /><br />$sql = 'SELECT * from tb where lid<50';<br />$re_materialTypeID = $db->getAll($sql);<br /><br />foreach($re_materialTypeID as $key => $val)<br />{<br /> $getMaterialType[$key]->CreateTime = $val['CreateTime'];<br /> $getMaterialType[$key]->IsDefault = $val['IsDefault'];<br /> $getMaterialType[$key]->UpdateTime = $val['UpdateTime'] ;<br /><br />}<br />var_dump($getMaterialType);<br />return $getMaterialType;<br />
class so_getMaterialType {<br /> var $CreateTime = '';<br /> var $IsDefault = '';<br /> var $UpdateTime = '';<br /> function __construct($a) {<br /> foreach($a as $k->$v) $this->$k = $v;<br /> }<br />}<br /><br />$p = array();<br />$sql = 'SELECT * from tb where lid<50';<br />$re_materialTypeID = $db->getAll($sql);<br /> <br />foreach($re_materialTypeID as $key => $val) {<br /> $p[] = new so_getMaterialType($val) ;<br />}
$getMaterialType = new so_getMaterialType ;
$p[] = $getMaterialType ;