Home > Backend Development > PHP Tutorial > foreach循环中$p[] = $MaterialType 的疑点

foreach循环中$p[] = $MaterialType 的疑点

WBOY
Release: 2016-06-13 11:51:32
Original
985 people have browsed it

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 />
Copy after login

------解决方案--------------------
第一段代码
$p 是一个含有若干个 so_getMaterialType 对象的数组

第二段代码
$getMaterialType 的第一个元素是 so_getMaterialType 对象,其余的是 stdClass 对象

显然你需要的是第一种写法的结果
因此可以
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 />}
Copy after login

------解决方案--------------------
 $getMaterialType = new so_getMaterialType ;
Copy after login

关键字new会生成一个内存空间,该内存的地址赋给$getMaterialType,即$getMaterialType是一个指向前面内存空间的地址。所以,对象$getMaterialType就是指针。

$p[] = $getMaterialType ;
Copy after login

因为第一种写法没有改变$getMaterialType的值,所有p[]里所有元素指向同一块内存地址,数据当然也就一样了。
而第二种写法中,每次都会生成一个新的内存空间,并把地址赋给$getMaterialType,所以p[]中元素指向的是不同的内存空间。

简单来说:
只有$getMaterialType =  ×××才会改变$getMaterialType值,$getMaterialType->属性 = ×××不会改变$getMaterialType的值

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template