drdu53813 2015-03-10 13:21
浏览 25
已采纳

php如何在for循环中重新声明类

Im using a library called PDFmerger , naturally, it merges pdfs together. I wish to run a cron every night that builds these to have them ready for the next day. The issue is when I run the code after the first one it fails because it is attempting to redeclare the class.

The for statement...

            foreach ($pdfs_to_build as $pdf) {

                // do stuff

                $this->merge_pdfs($pdf['pdf_id']);

            }

And my pdf merging code that gets ran in the loop...

$pdf = new PDFMerger;

$pdf->addPDF(DIR_BOOKS.$book_path.'/static-pages/page-1.pdf', '1'); 
$pdf->addPDF(DIR_FINAL_PDFS.$pdf_id.'/custom-page-1.pdf','1'); 
$pdf->addPDF(DIR_BOOKS.$book_path.'/static-pages/page-2.pdf', '1'); 

$pdf->merge('file', DIR_FINAL_PDFS.$pdf_id.'/final-build-'.$pdf_id.'.pdf');

Error after the first 1 is complete and we've onto the next one...

Fatal error: Cannot redeclare class PDFMerger in /var/www/example/pdfmerger/PDFMerger.php on line 24

Im wondering is there a workaround for this?

  • 写回答

2条回答 默认 最新

  • duanci8209 2015-03-10 13:25
    关注

    The problem isn't that you're calling new PDFMerger twice, that's perfectly acceptable. The problem is that the file:

    /var/www/example/pdfmerger/PDFMerger.php
    

    Is being include'd (or otherwise executed) twice, which has on line 24:

    class PDFMerger {
    

    That is what is triggering your error, that class has already been declared. Figure out why that file is being included twice and your error will clear up.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题