2009-08-18 82 views
0

我正在擴展FPDF並希望包含一些外部變量,但它不起作用。我究竟做錯了什麼?擴展PHP類不接受變量

<?php 

class PDF extends FPDF { 

    public var $DocNum; 
    public var $cliente; 
    public var $noCliente; 
    public var $noTienda; 
    public var $noPedido; 

    function Header() { 
     $this->Image('fpdf/cabMCR.jpg',5,10,100,0,'JPG',''); 

     $this->SetFont('Arial','B','16'); 
     $this->SetXY(125,15); 
     $this->Write(5,'FACTURA'); 

     // doing some more stuff here 
    } 

    function Footer() { 
     $this->SetTextColor(150,150,150); 
     $this->SetXY(10,285); 
     $this->SetFont('Helvetica','I',7); 
     $this->Write (5, 'MCR Electronic S.L. Inscrita en el Registro Mercantil de Madrid en Tomo 15819, folio 163, Seccion 8ª, hoja M-267058, CIF: B82766452'); 
     $this->SetTextColor(0,0,0); 
    } 
} 

$pdf=new PDF(); 

$pdf->Open(); 
$pdf->AddPage('P'); 
$pdf->SetDisplayMode(real,'default'); 
$pdf->SetAuthor('Company S.L'); 
$pdf->SetTitle('Factura MCR'); 

$pdf->$DocNum = $DocNum; 
$pdf->$cliente = $cliente; 
$pdf->$noCliente = $noCliente; 
$pdf->$noTienda = $noTienda; 
$pdf->$noPedido = $noPedido; 

?> 
+0

請更具體地說明它是如何「不工作」的。 – 2009-08-18 19:00:39

+2

您可能希望移除'var',因爲它已被棄用 – 2009-08-18 19:00:46

+0

@ Chacha102,它已從PHP 5.3撤銷棄用 – 2009-08-18 19:09:17

回答

3

你不想引用類成員的$,除非你對變量做一些奇怪的事情。

取而代之的是:

$pdf->DocNum = $DocNum; 
$pdf->$cliente = $cliente; 
$pdf->$noCliente = $noCliente; 
$pdf->$noTienda = $noTienda; 
$pdf->$noPedido = $noPedido;  

應該更多這樣的:

$pdf->DocNum = 'somevalue'; 
$pdf->cliente = 'somevalue'; 
$pdf->noCliente = 'somevalue'; 
$pdf->noTienda = 'somevalue'; 
$pdf->noPedido = 'somevalue'; 
+0

如果變量中存在多於1個'$',它會嘗試查找該變量以創建一個變量變量。 – 2009-08-18 19:02:32

+0

我這樣做就像你說的,我不再有錯誤,但我也沒有得到通過的變種... – mistero 2009-08-18 19:23:34

+0

你需要改變我把'somevalue'在右側的實例無論您想要設置這些班級成員的值/變量。 – AvatarKava 2009-08-18 19:27:19

0

你錯了!應該是這樣的:

$pdf->DocNum = $DocNum; 
$pdf->cliente = $cliente; 
$pdf->noCliente = $noCliente; 
$pdf->noTienda = $noTienda; 
$pdf->noPedido = $noPedido; 

Suerte!