Hello. I broke my head, help with advice. I make the generation of XML files through gearman. In the worker, there is a function that loops around the names of the templates and inserts them inside the function. The problem is that the templates, though different, have the same functions (ie, the names of the functions are the same). PHP throws out a Fatal error when the require is repeated.

function regenirationXMLsFreeBoards($job){ //Запуск задачи ... foreach($FreeBoards as $Board){ //Обходим бесплатные доски if(!generationXMLFile($Board)) break; } ... } function generationXMLFile($Board){ $dir = "../../xmls/"; $BoardName = $Board["name"]; $template = $dir."templates/".$BoardName.".php"; require $template; createXML($fileXML); //Эта функция из шаблона $template ... } 

Those. the first cycle is successful, on the second we fall off due to an error

PHP Fatal error: Cannot redeclare createXML ()

There can be a lot of patterns, while only two. Help advice what to do. It only occurred to me to create a separate function in the calculation that once again causing it, we would lose the previous require. But I forgot that require is inserted into the script at the first use and remains for the entire lifetime of the script.

If something is not clear, ask, I will explain.

    2 answers 2

    In include files instead of:

     function createXML() { // .... } 

    Do:

     $createObjectXML = function () { // что нужно }; $createXML = function () use ($createObjectXML) { $var = $createObjectXML(); // ... }; 

    Then:

     require $template; $createXML($fileXML); 

    Since this is not a function, but a variable, there will be no conflicts.

    If you just need to solve the problem, then the option above will do. If it is necessary that it is good, then it would be better to raise the stakes in the game of abstraction, and get separate named objects with the necessary methods for each report or what you do. Anyway, these classes and files will be edited by the programmer.

    • I also thought about it, but for now I have no idea how it will work. - Maxim Mezentsev
    • This is how it works. I checked before writing the answer. - sanmai
    • And how now to receive return from function? Fatal error: Function name must be a string in ... on line 738 738: return $result; - Maxim Mezentsev
    • As in any ordinary function, only $ at the beginning of the name. - sanmai
    • Why then do I get Fatal error: Function name must be a string in ... on line 738 ? - Maxim Mezentsev

    Three options (but the essence is the same - to achieve different "full qualified" names for the functions)

    1. Change function names so that they do not match.
    2. Use different namespaces
    3. Refactor and put these functions into methods of different classes.