Leo
Leo

Reputation: 39

PHP get a variable from outside a class without using global

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');     
        }      
    }
?>

Upvotes: 0

Views: 114

Answers (2)

Nemoden
Nemoden

Reputation: 9056

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.

Upvotes: 0

Patrick Moore
Patrick Moore

Reputation: 13344

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 );
?>

Upvotes: 1

Related Questions