我的应用程序正在构建 PDF 文档。它使用脚本来生成每个页面的 HTML。PDF-Generating 类是“Production”,页面类是“Page”。
class Production
{
private $_pages; // an array of "Page" objects that the document is composed of
public getPages()
{
return $this->_pages;
}
public render()
{
foreach($this->_pages as $page) {
$pageHtml = $page->getHtml($this); // Page takes a pointer to production to access some of its data.
}
}
}
这是 Page 类摘要:
class Page
{
private $scriptPath; // Path to Script File (PHP)
public function getHtml(Production &$production)
{
$view = new Zend_View();
$view->production = $production;
return $view->render($this->scriptPath);
}
}
我在编码目录时遇到了问题。它访问 Production,获取所有页面,查询它们,并根据页面标题构建 TOC:
// TableOfContents.php
// "$this" refers to Zend_View from Pages->getHtml();
$pages = $this->production->getPages();
foreach($pages as $page) {
// Populate TOC
// ...
// ...
}
发生的情况是 TableOfContents.php 中的 foreach 干扰了生产中的 foreach。生产 foreach 循环在索引页(实际上是文档中的第二页,在封面页之后)处终止。
文档布局是这样的:
1) 封面
2) 目录
3) 页面 A
4) 页面 B
5) 页 C
TableOfContents.php 在其 foreach 循环中根据需要遍历页面并构建整个文档的索引,但 Production 中的循环在目录处终止并且不会继续呈现页面 A、B 和 C。
如果我从 TableOfContents.php 中删除 foreach,所有连续的页面都会正确呈现。
我觉得这是指针和变量范围的问题,那么我该怎么做才能解决它?