函数不工作(FPDF)内的类对象


Class Object inside Function Not Working (FPDF)

我使用的是FPDF(加上FPDI)。

我有一个这样的代码:

$pdf->setSourceFile("source.pdf");
$tplIdx = $pdf->importPage(1);
$size = $pdf->useTemplate($tplIdx, 1, 1, 5.4);

结果:效果良好。

但是当我把代码封装在一个函数中时:

function hello(){
$pdf->setSourceFile("source.pdf");
$tplIdx = $pdf->importPage(1);
$size = $pdf->useTemplate($tplIdx, 1, 1, 5.4);
}
hello();

结果:

Fatal Error: Call to a member function setSourceFile() on a non-object

由于某种原因,$pdf对象在函数内部时无法工作。

知道为什么吗?

@qrafzvzv,您需要在函数中传递pdf对象作为参数。

For Example :
function hello($pdf) {
    $pdf->setSourceFile("source.pdf");
    $tplIdx = $pdf->importPage(1);
    $size = $pdf->useTemplate($tplIdx, 1, 1, 5.4);
}
hello($pdf);