2014-10-29 2 views
1

Я использую TCPDF для создания генератора призов. проверяет тип награды и основанный на том, что он заполняет в переменной image_path (и некоторые другие переменные, которые не имели никакого отношения к этому вопросу.PHP использует внешнюю переменную в расширяющемся классе

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

Чуть дальше, как видно в example_051 из TCPDF они проходят класс, чтобы определить фоновое изображение. путь к этому образу является тот, который находится в переменном $ 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 не известны, в рамках расширения. является я мог бы сделать эту работу?

Заранее благодарим!

ответ

0

Посмотрите на $GLOBALS переменных здесь пример

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

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

Sum(); 
echo $b; 
?> 

Подробнее о переменных SCOPES here

+0

После замены $ img_file на $ GLOBALS [ ''] img_file в продолжении этого класса переменной остается пустым т.е. матрица ($ GLOBALS [ 'img_file']); в конце расширения. – Yentel

1
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); 
+0

Как-то это приводит к ошибке TCPDF: пустое семейство шрифтов http://pastebin.com/8DekH7KL – Yentel

+0

Возможно, вам потребуется вызвать родительский конструктор. – Evert

0

Вам нужно сделать переменную $ 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(); 

    ... 
0

Это работает для меня.

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); 
Смежные вопросы