In general when your code needs "something" we are saying this code has a dependency on "something". In your case MYPDF::Footer() has a dependency on $invoice_number.
There are generally 3 ways of providing a dependency that you might be interested in in your case:
- via
__constructor:
class MYPDF
{
protected $invoice_number;
public function __constructor($invoice_number)
{
$this->invoice_number = $invoice_number;
}
public function Footer()
{
$this->Cell(0, 10, "Invoice: {$this->invoice_number}", 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
$pdf = new MYPDF($invoice_number);
$pdf->Footer();
- via a setter method
class MYPDF
{
protected $invoice_number;
public function setInvoiceNumber($invoice_number)
{
$this->invoice_number = $invoice_number;
}
public function Footer()
{
$this->Cell(0, 10, "Invoice: {$this->invoice_number}", 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
$pdf = new MYPDF();
$pdf->setInvoiceNumber($invoice_number);
$pdf->Footer();
- via argument to a method:
class MYPDF
{
public function Footer($invoice_number)
{
$this->Cell(0, 10, "Invoice: {$invoice_number}", 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
$pdf = new MYPDF();
$pdf->Footer($invoice_number);
I'd suspect you'd be better off with either 1 or 2.
If your dependency is "hard" (i.e. you ABSOLUTELY NEED this in order for your code to function), then go with 1.
Otherwise you could go with 2 (in case if invoice number is arbitrary and you will be checking it in MYPDF::Footer... so it's a "soft" dependency).
In case if you wanna use same instance of MYPDF to generate multiple footers with different invoice numbers, go with 3.
BTW, google up why globals are bad. If I were you I'd be less concerned about how to overcome using global, and more concerned around WHY globals are bad.
$invoice_numberas an argument to methodFooter(). Sopublic function Footer($invoice_number) { ... }. And pass it when you call it:instance_name.Footer($invoice_number).public functionfrom outside, which returns the variable's value, that you need, or a wholeobject/array