Recursive fetch with phprenderer - redeclare error

i have a few templates that print recursive summaries

im using the slim renderer that comes with the slim framework - \Slim\Views\PhpRenderer

im trying to do a bunch of recursive summaries in a set, i fetch the template output and dump it into a file and then turn the file into a pdf

all the pages with recursive summaries work fine on the individual pages and the export to PDF also works fine with these pages

but when i try to do the batch set and do the same template with its recursive method more than once i have trouble

the first result of fetch makes it into the html file and the PDF is generated fine but it errors on the second one saying:

“Fatal error: Cannot redeclare printSummaryTree()”

which is just a basic recursive function in the template, i have many templates with the same function name it doesnt cause any issues

‘function printSummaryTree($tree, $depth)’

its like the method defined in the previous template fetch is still in scope or something?

in the class that calls fetch, i tried to get a complete new renderer right before calling fetch, no affect

…$this->view = new \Slim\Views\PhpRenderer(‘path/to/templates’)
$output = $this->view->fetch(’…

if i follow the method call into the renderer i can see where it includes the template

if i change it from:

protected function protectedIncludeScope ($template, array $data) {
include $template;
protected function protectedIncludeScope ($template, array $data) {
include_once $template;

the script will run to completion but only the first fetch is successful, all the html is blank in the other files

anyone know where i am going wrong? how do i clear the scope/cache whatever it is?

im not really sure what else i can do here i guess its just a combination of my design choice and the fact that these php calls in the renderer are just some simple php calls

the template being included with require means any function defined within a template is in global scope for the duration of the script?

ive just given every one of these recursive summary template methods a unique name and prefaced the method with a function not exists check… at least if every method is unique i know i can omit declaring the function without worry for which template it might have come from

maybe an anonymous method might be more appropriate

This sound like you are including a PHP file with this funtions twice. PHP cannot redeclare a funtion that is already declared. Try to use “require_once” instead of “require”. Or better use classes and the PSR-4 (composer) autoloader.

thanks odan :ok_hand:
appreciate it mate

if anyone is actually interested i found a better option, it is to assign the function to a variable and execute that passing it in with use, then it lives in the same scope as the other variables for the template and is forgotten after the template is rendered

$printSummaryTree = function($tree, $depth) use (&$printSummaryTree) {


only other option would probably be to use a more complicated renderer that does its own interpretation instead of a simple require, which i don’t have any interest in doing at this point