Arrays of Classes in PHP

Started by
1 comment, last by Monder 20 years, 11 months ago
Can someone please explain how you can create an array of classes in PHP? I have a while loop that should add a class with data in it into an array every iteration. The way I add stuff into the array is this

$Fields = new EditField;
$Fields->Name = $result_array['Field'];
  </pre>  

Later &#111;n in the same while loop I have this (I added it as a test)

<pre>
echo $Fields->Name;
echo "< BR >";
echo $Fields->Hidden;
echo "< BR >";
echo $Fields->Type;
echo "< BR >";
  </pre>  

Hidden and Type are both other variables in the class.  This echo's out all the data correctly.  Now later &#111;n in the script (that lot was before the <HTML> tag this next bit is in the <BODY>&lt;/body> tags) I have this

<pre>
for($i = 0;$i < $ArraySize;$i++)
{
	printf("&lt;tr>&lt;td>%s&lt;/td>&lt;td>%s&lt;/td>&lt;td>%s&lt;/td>&lt;/tr>", $Fields[$i]->Type, $Fields[$i]->Name, $Fields[$i]->Hidden);
}
  </pre>  

$ArraySize is set correctly and everything but for some $Fields[$i]->Type, $Fields[$i]->Name and $Fields[$i]->Hidden all have nothing in them.  Can anyone tell me what's going wrong here?   </i> 

[edit]Fixed < br > formatting (in the real code there aren't any spaces in the tag[/edit]

<SPAN CLASS=editedby>[edited by - Monder &#111;n May 7, 2003 6:59:33 PM]</SPAN>    
Advertisement
wouldn''t it be

$Fields[10] = new EditField;

?
Sup guys?
I've just realised that I should be using

$Fields = new EditField;$Fields[$i]->Name = $result_array['Field']; 


and

echo $Fields[$i]->Name;echo "< BR >";echo $Fields[$i]->Hidden;echo "< BR >";echo $Fields[$i]->Type;echo "< BR >"; 


But when I do that the array still doesn't get filled. I must be doing something wrong. Can someone tell me what it is?

[edited by - Monder on May 8, 2003 1:36:55 PM]

This topic is closed to new replies.

Advertisement