MonkeyBusiness
MonkeyBusiness

Reputation: 593

Use variable into php public php function

I'm new to php. I have:

require_once('tcpdf_include.php');

    $poruka = $user['porukanadnu'];

class MYPDF extends TCPDF {


    // Page footer
    public function Footer() {

        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 10, 'Page '.$poruka, 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
}

Why I cant use my variable $poruka into public function? And how to make it possbile?

Upvotes: 1

Views: 72

Answers (1)

Andrew
Andrew

Reputation: 20081

You are trying to use $poruka in a class, but it is out of scope there, you could use it in the class if you first declare it to be of global scope, eg:

require_once('tcpdf_include.php');

$poruka = $user['porukanadnu'];

class MYPDF extends TCPDF {


    // Page footer
    public function Footer() {
        global $poruka;     // This will let you use $poruka.

        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 10, 'Page '.$poruka, 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
}

Alternatively you could pass it into the function, like this:

require_once('tcpdf_include.php');

$poruka = $user['porukanadnu'];

class MYPDF extends TCPDF {


    // Page footer
    public function Footer($poruka) {

        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 10, 'Page '.$poruka, 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
}

but then when you called the function you'd have to include it as an attribute:

   Footer($poruka);

Upvotes: 1

Related Questions