sprain/php-swiss-qr-bill

Remove strict Fpdf Class name

kasoft opened this issue · 3 comments

I would be happy if the Classname Fpdf could be removed for more practical use. FPDF is very old and i mixed my own construction wich is ending in a different Class Name (=MyPDF). Maybe a lot of people also did. So this Class will not be accepted by FpdfOutput($qrBill, 'en', $pdf);

private $fpdf;
   private float $offsetX;
   private float $offsetY;

   public function __construct(
       QrBill $qrBill,
       string $language,
       $fpdf,
       float $offsetX = 0,
       float $offsetY = 0
   ) {
       parent::__construct($qrBill, $language);
       $this->fpdf = $fpdf;
       $this->offsetX = $offsetX;
       $this->offsetY = $offsetY;
       $this->setQrCodeImageFormat(QrCode::FILE_FORMAT_PNG);
   }

Hi

If I understand correctly, you are extending the Fpdf\Fpdf-class. PHP type declarations support extension, therefore it should be no problem to pass a child class of Fpdf.

So make sure you declare your class correctly

class KasoftFpdf extends Fpdf\Fpdf
{
    function __construct($orientation='P', $unit='mm', $size='A4')
    {
        parent::construct(func_get_args());
       // your code...
    }
}

Hi,

thanks for this solution and the quick answer. I think i understood your aproach a little bit more. Anymway - if you download the Version from fpdf.org without Namespace you'll also have not "Fpdf" but "FPDF". What means you have to rewrite your code to fit the needs of your great Project (thanks for that!).

If you do not use the version from composer, it should be even simpler.

Just write a wrapper like this

<?php
namespace Fpdf

use KasoftFpdf

class Fpdf extends KasoftFpdf
{}

Then map the directory containing this namespace in composer.json→autoload.psr-4. This class will now perfectly imitate Fpdf\Fpdf.