0

The code below is working, but reading some answers here in the forum everyone is saying not to use "global", how can I change the code below without using it?

<?php
    $invoice_number = "1234";

    class MYPDF extends TCPDF 
    {
        public function Footer() 
        {
            global $invoice_number;
            $this->Cell(0, 10, 'Invoice: ' . $invoice_number, 0, false, 'C', 0, '', 0, false, 'T', 'M');     
        }      
    }
?>
2
  • 1
    Put $invoice_number as an argument to method Footer(). So public function Footer($invoice_number) { ... }. And pass it when you call it: instance_name.Footer($invoice_number). Commented Sep 8, 2020 at 1:08
  • you can use a public function from outside, which returns the variable's value, that you need, or a whole object/array Commented Sep 8, 2020 at 1:09

2 Answers 2

1

Build a helper function to set the variable. Keep the variable within the class as a protected (or public) variable, depending on your needs.

<?php
    $invoice_number = "1234";

    class MYPDF extends TCPDF 
    {
        protected $invoice_number;
        public function Footer() 
        {
            $this->Cell(0, 10, 'Invoice: ' . $this->invoice_number, 0, false, 'C', 0, '', 0, false, 'T', 'M');     
        }      
        public function setInvoiceNumber( $val ){
            $this->invoice_number = $val;
        }
    }

    // Then elsewhere in your code:
    $pdf = new MYPDF();
    $pdf->setInvoiceNumber( $invoice_number );
?>
Sign up to request clarification or add additional context in comments.

Comments

0

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:

  1. 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();
  1. 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();
  1. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.