簡體   English   中英

在PHP公共php函數中使用變量

[英]Use variable into php public php function

我是php新手。 我有:

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

為什么我不能在公共函數中使用變量$poruka 以及如何使其成為可能?

您試圖在一個類中使用$poruka ,但是它超出了范圍,如果您首先聲明它是全局范圍的,則可以在類中使用它,例如:

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

或者,您可以將其傳遞給函數,如下所示:

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

但是,當您調用該函數時,必須將其作為屬性包括在內:

   Footer($poruka);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM