Jump to content

Create an object in a loop?


christo16

Recommended Posts

I realize this may be a better post for the oop forum but I figured there is more visibility here. I have a class that outputs an .ics (iCal file) and I want to create multiple .ics files in the same page.  I've setup a loop to create a new instance of the class but it creates one and then doesn't output anymore.  I figure because its trying to instantiate the object with the same variable.  Whats the best way to setup the loop so that I am creating new object on each loop?

Thank you!

Link to comment
Share on other sites

Some php code may help, but do you use a certain pattern. I think a factory pattern is quite usable in your case:

class icalGenerator{
  public function __construct(){}

  public function createIcal(){
    $ical = new iCal();
    $ical->doSomething();
    return $ical;
  }
}

class ical{
  public function __construct(){}
  public function __toString(){}
  public function doSomething(){}
}

You can use the classes in a loop:

$result = array( 12, 14, 19, 40, 89 ); //A list of event ids, for example.
$generator = new icalGenerator();

if( $result ){
  foreach( $result as $event ){
    $ical = $generator->createIcal( $event );
    echo $ical;
  }
}else
  throw new Exception( 'No events to display!' );

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.