简体   繁体   English

PHP在扩展类中使用外部变量

[英]PHP use an external variable in an extending class

I'm using TCPDF to make an award generator. 我正在使用TCPDF制作奖励生成器。 It checks the type of award and based on that it fills in the image_path variable (and some other variables that had nothing to do with this question. 它检查奖励的类型,并根据它填充image_path变量(以及与此问题无关的其他一些变量)。

switch($award){
    case 25:
        $img_file = '../../img/award_25.png';
        break;
    case 50:
        $img_file = '../../img/award_50.png';
        break;
    ... and so on ...
}

A little further, as seen in example_051 of TCPDF they extend the class to define the background image. 再进一步一点,如TCPDF的example_051所示,它们扩展了类以定义背景图像。 The path to that image is the one that is in the variable $img_file created above. 该图像的路径是在上面创建的变量$ img_file中的路径。

require_once('tcpdf_include.php');
class MYPDF extends TCPDF {
    public function Header() {
        // get the current page break margin
        $bMargin = $this->getBreakMargin();
        // get current auto-page-break mode
        $auto_page_break = $this->AutoPageBreak;
        // disable auto-page-break
        $this->SetAutoPageBreak(false, 0);
        // margins Left, Top, Right, Bottom in pixels
        $this->Image($img_file, -9, -8, 316, 225, '', '', '', false, 300, '', false, false, 0);
        // restore auto-page-break status
        $this->SetAutoPageBreak($auto_page_break, $bMargin);
        // set the starting point for the page content
        $this->setPageMark();
    }
}

Due to the scope the variable $image_file is not known within the extend. 由于范围的原因,扩展内的变量$ image_file是未知的。 Is there a way I could make this work? 有什么办法可以使我工作吗?

Thanks in advance! 提前致谢!

class MYPDF extends TCPDF {
protected $img_file;
 public function __construct($img_file)
    {
        $this->img_file = $img_file;
    }
    public function Header() {
        // get the current page break margin
        $bMargin = $this->getBreakMargin();
        // get current auto-page-break mode
        $auto_page_break = $this->AutoPageBreak;
        // disable auto-page-break
        $this->SetAutoPageBreak(false, 0);
        // margins Left, Top, Right, Bottom in pixels
        $this->Image($img_file, -9, -8, 316, 225, '', '', '', false, 300, '', false, false, 0);
        // restore auto-page-break status
        $this->SetAutoPageBreak($auto_page_break, $bMargin);
        // set the starting point for the page content
        $this->setPageMark();
    }
}
$MYPDF = new MYPDF($img_file);

Take a look at $GLOBALS variables here is a example 看一下$GLOBALS变量,这里是一个例子

<?php
$a = 1;
$b = 2;

function Sum()
{
    $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
} 

Sum();
echo $b;
?>

More on Variables scopes here 更多关于变量的范围在这里

You need to make variable $img_file a global for use inside the function. 您需要使变量$ img_file成为全局变量,以在函数内部使用。 This needs to be done inside the function Manual here: http://php.net/manual/en/language.variables.scope.php 这需要在此处的功能手册中完成: http : //php.net/manual/zh/language.variables.scope.php

public function Header() {
    global $img_file;
    // get the current page break margin
    $bMargin = $this->getBreakMargin();

    ...

This worked for me. 这对我有用。

class MYPDF extends TCPDF {
  private $data = array();

  public function setData($key, $value) {
    $this->data[$key] = $value;
  }

  public function Header() {
    $this->data['invoice_no'];
    .... 
  }
}    
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 
$pdf->setData('invoice_no', $invoice_no);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM