2017-08-17 38 views
0

我正在嘗試編寫一個可以調用多個類的函數,每個函數都會將一個小部件添加到單個PDF文件,然後返回整個PDF文件。在多個類中使用TCPDF

這裏的構造函數的代碼:

<?php 

class PdfConstructor { 

    protected $logger; // I'm sure there's another way to use the logger located in the public dependencies without having to initalise it here 

    public function __construct($logger){ 
     $this->logger = $logger; 
    } 

    public function studentPdf(){ 
     $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 

     // --------------------------------------------------------- 
     require 'pdfwidgets/studentheader.php'; // TODO: Need to load this via autoloader 
     $headerController = new StudentHeader($pdf); 
     $headerInfo = $hederController->getHeader(); 

     // --------------------------------------------------------- 

     //Close and output PDF document 
     $pdf->Output('C:\xampp7\htdocs\edquire-api\logs\test.pdf', 'F'); 
    } 
} 
studentheader

然後,我有這樣的:

<?php 

class StudentHeader { 

    protected $pdf; 

    public function __construct($pdf){ 
     $this->$pdf = $pdf; 
    } 

    public function getHeader(){ 
     $this->pdf->AddPage(); 
    } 
} 

我想給$pdf對象傳遞給studentheader但我得到的錯誤:

Catchable fatal error: Object of class TCPDF could not be converted to string in C:\xampp7\htdocs\edquire-api\classes\pdfwidgets\studentheader.php on line 8

它爲什麼試圖將其轉換爲字符串,並有沒有更好的方法來實現使用小部件構建PDF?

+1

投票關閉,因爲你只是要求我們找到你的錯字。 –

回答

1

這是8號線:

$this->$pdf = $pdf; 

它應該是:

$this->pdf = $pdf; 

它說,它不能轉換$pdf爲字符串,因爲屬性名稱必須是一個字符串。你在那裏用它作爲屬性名稱。

腳註:如果您沒有「收到」錯誤消息,Google始終是一個很好的策略。我搜索了「可捕捉的致命錯誤:類TCPDF的對象無法轉換爲字符串」,最高的結果都指向正確的方向。