假设你有一个包含对象的数组:
array
0 =>
object(User\Entity\User)[297]
public 'id' => int 1
private 'first_name' => string 'Peter' (length=5)
private 'last_name' => string 'Johnson' (length=7)
private 'initials' => string 'J.J.' (length=4)
private 'email' => string 'test@gmail.com' (length=21)
1 =>
object(User\Entity\User)[296]
public 'id' => int 2
private 'first_name' => string 'Edith' (length=8)
private 'last_name' => string 'Peters' (length=7)
private 'initials' => string 'R.J.' (length=4)
private 'email' => string 'edit@gmail.com' (length=26)现在我想把它们放在一张桌子上。但由于我想让它变得普遍,我尝试用一种抽象的方式来做。
我在下面的函数中有一个$aColnames数组,它只能显示我想要在表中看到的字段的值。
这是我试图构建的方法:
private function generateTable()
{
foreach($this->aData as $aData){
$this->sTable.= '<tr>';
foreach($this->aColnames as $sColname){
$this->sTable.= '<td>';
/****What code goes here ****/
$this->sTable.= '</td>';
}
$this->sTable.= '</tr>';
}
}问题是,我如何从对象中获取值?我每次都需要实例化对象吗?有谁能帮帮我吗?
发布于 2012-04-27 02:54:46
不需要,您不需要再次实例化对象。当你首先把它们放在数组中时,它们就被实例化了!
但是,您需要将这些变量声明为公共变量,以便可以访问它们,或者使用某种类型的getter函数。
https://stackoverflow.com/questions/10339477
复制相似问题