homepage Welcome to WebmasterWorld Guest from 54.205.189.156
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Looping through existing objects of a class
Using print()?
Torniojaws




msg:4428984
 10:43 am on Mar 14, 2012 (gmt 0)

For practice, I'm trying to print all concert data and split them up depending on if they are an upcoming/future show or a past show. Here is an example:

$liveShow7 = new Concert("2013-07-23", "Rovaniemi", "Finland", "Grande", "The Howl, Sinfect");
$liveShow6 = new Concert("2010-03-31", "Oulu", "Finland", "Hevimesta", "Synesthesia");
$liveShow5 = new Concert("2009-12-03", "Raahe", "Finland", "Rytmikellari", "-");


If needed, I can post the full code, but at the moment everything works when I call the above in this manner:

<?php $liveShow2->show("past"); ?>
<?php $liveShow5->show("past"); ?>


The show() method is used to select the type of shows to display ("upcoming" | "past"), and works also.

Now, what I want to do, is to loop through all of those n+1 instances of $liveShow# (where # = number 1, 2, 3, ..., total amount). This is what I think should do it, but alas, doesn't:


for($j=1; $j>=$numberConcerts; $j++) {
print ${"liveShow".$j}->show("past"); // I also tried ${$liveShow.$j and ${$"liveShow".$j)
}


What I'm hoping for it to do is to generate these calls as PHP code and run them, if 0<$j<5:

$liveShow1->show("past");
$liveShow2->show("past");
$liveShow3->show("past");
$liveShow4->show("past");



But it seems like it ignores the method call, and displays nothing. Any tips? Thanks!

 

eelixduppy




msg:4429011
 12:58 pm on Mar 14, 2012 (gmt 0)

Just out of curiosity, why aren't you storing these in an array of objects?

e.g.:


$shows = array();
$shows[] = new Concert("2013-07-23", "Rovaniemi", "Finland", "Grande", "The Howl, Sinfect");
$shows[] = new Concert("2010-03-31", "Oulu", "Finland", "Hevimesta", "Synesthesia");
$shows[] = new Concert("2009-12-03", "Raahe", "Finland", "Rytmikellari", "-");

foreach($shows as $show) {
$show->show("past");
}


IMO, there is never a good reason to use variable variables like this.

Torniojaws




msg:4429019
 1:16 pm on Mar 14, 2012 (gmt 0)

Ah, that sounds like a good way to do it, thanks! The only reason I didn't is that I wasn't aware of how to do it :) Self-learning by search engines is sometimes quite frustrating...

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved