Extending PEAR's File_PDF

Took me a bit of time to figure this one out.

There's a couple of empty functions in the PEAR File_PDF class (based on FPDF), header and footer, that you can implemented with inheritance to define a header and footer for your PDF pages. Very useful of course. However I ran into the following problem: I tried extending the class as suggested in the function's comments, for example:

class My_PDF extends File_PDF {

function footer()
{
// Go to 1.5 cm from bottom
$this->setY(-15);
// Select Arial italic 8
$this->setFont('Arial', 'I', 8);
// Print centered page number
$this->cell(0, 10, 'Page ' . $this->getPageNo(), 0, 0, 'C');
}
}

And of course changed the call to My_PDF:

$pdf = &My_PDF::factory(array('orientation' => 'P', 'format' => 'A4'));

But nothing happened. Even custom functions I defined didn't work, didn't seem to exist. The clue is the "factory" constructor function. This is where the class is actually called. So even My_PDF::factory eventually called File_PDF instead, setting my own extended class aside. I googled some and found an old bug report talking about the same problem. The problem was solved by adding a class variable to the factory function call, so instead, you do this:

$pdf = &My_PDF::factory(array('orientation' => 'P', 'format' => 'A4'),'My_PDF');

Now the factory function calls the class given (File_PDF by default of course), and voila, your own functions work. You can even just replace the first My_PDF with File_PDF, that doesn't make a difference, as long as you give your class name along with the factory function call.

Hope this helped!