title: Multiple PDFs in Loop with FPDF
body: <p>I have a FDPF function that creates a standard PDF for various parts of the website. It simply takes some data in, creates a multi page PDF and returns the PDF object e.g.</p>
  
  <pre><code>function pdfBuild($orderData) {
  
  
  
  class PDF extends FPDF
  {
  // Page header
  function Header()
  {
      // Logo
      $this-&gt;Image('logo.png',10,6,30);
      // Arial bold 15
      $this-&gt;SetFont('Arial','B',15);
      // Move to the right
      $this-&gt;Cell(80);
      // Title
      //$this-&gt;Cell(80,10,'Game Sheet',1,0,'C');
      // Line break
      $this-&gt;Ln(20);
  }
  
  // Page footer
  function Footer()
  {
      // Position at 1.5 cm from bottom
      $this-&gt;SetY(-15);
      // Arial italic 8
      $this-&gt;SetFont('Arial','I',8);
      // Page number
      $this-&gt;Cell(0,10,'Page '.$this-&gt;PageNo().'/{nb}',0,0,'C');
  }
  
  
  
  }
  
  $pdf = new PDF();
  $pdf-&gt;AliasNbPages();
  $pdf-&gt;SetFont('Times','',12);
     for($a=0;$a&lt;count($orderData);$a++) {
      $pdf-&gt;AddPage();
      //Add stuff to PDF
     }
  return $pdf;
  
  }
  </code></pre>
  
  <p>My issue arises in a loop that emails individual PDFs out to clients e.g</p>
  
  <pre><code>function buildPDFs() {
  
      //Get a location
      $query = "GET SOME CLIENT INFORMATION";
      $result = getSQLConnection($query);
      if ($result-&gt;num_rows &gt; 0) {
  
              while($row = $result-&gt;fetch_assoc()) {
  
                  //Get some info from dB using $row
  
                  //Format info from db into $data variable
  
                  $pdf = pdfBuild($data);
  
                  //Test Data
                  $to = 'stackoverflow@question.com';
                  $from = 'hello@clientinfo.com';
                  $subject = 'PDFs'
                  $message = 'Howdy';
                  //End
                  emailPDF($pdf, $to, $from, $subject, $message);
  
              }
      }
  
      echo 'Done';
  
  }
  </code></pre>
  
  <p>The problem is that if I do two iterations of the loop, the attachment is the PDF that was generated in the first instance but I get two emails with the same attachment. As such it would appear the $pdf object never changes in the loop, even though I am certain the data being passed to the PDF generation function each time is different. The generated PDFs are identical.</p>
  
  <p>Do I need to unset or otherwise 'destroy' the PDF object each time in the loop?</p>
  
  <p><strong>Update</strong></p>
  
  <p>The removal of Class definition of Header and Footer, as in the above code snippet, solves the problem. But I can't understand why.</p>
  
  <p><strong>Question</strong>: Why does the removal of the 'class extend' code solve the problem and allow the loop to generate different PDFs each time, as expected, and email them correctly?</p>
  
  <p><strong>Answer below from Veve and solves the problem. I was mis-declaring and completely misusing the Class</strong></p>