【问题标题】:PHP use an external variable in an extending classPHP 在扩展类中使用外部变量
【发布时间】:2014-12-25 10:20:06
【问题描述】:

我正在使用 TCPDF 制作奖励生成器。 它检查奖励的类型,并根据它填充 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 ...
}

更进一步,如 TCPDF 的 example_051 所示,它们扩展了类以定义背景图像。 该图像的路径是上面创建的变量 $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();
    }
}

由于范围的原因,变量 $image_file 在扩展中是未知的。有什么方法可以让我完成这项工作吗?

提前致谢!

【问题讨论】:

    标签: php class variables tcpdf extend


    【解决方案1】:

    看看$GLOBALS变量 这是一个例子

    <?php
    $a = 1;
    $b = 2;
    
    function Sum()
    {
        $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
    } 
    
    Sum();
    echo $b;
    ?>
    

    更多关于变量作用域here

    【讨论】:

    • 在此类的扩展中将 $img_file 替换为 $GLOBALS['img_file'] 后,变量仍然为空,即 die($GLOBALS['img_file']);在扩展的末尾。
    【解决方案2】:
    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);
    

    【讨论】:

    • 不知何故这会导致一个 TCPDF 错误:空字体系列pastebin.com/8DekH7KL
    • 您可能需要调用父构造函数。
    【解决方案3】:

    您需要将变量 $img_file 设为全局变量以在函数内部使用。 这需要在函数内部完成 手册在这里: http://php.net/manual/en/language.variables.scope.php

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

    【讨论】:

      【解决方案4】:

      这对我有用。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-15
        • 1970-01-01
        • 2014-08-24
        • 1970-01-01
        • 1970-01-01
        • 2010-09-23
        相关资源
        最近更新 更多