2017-08-17 39 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的对象无法转换为字符串”,最高的结果都指向正确的方向。