Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Include files without inhereting variables?

Is it possible to include (or, for my uses, run) another PHP file from within a PHP script without inheriting the variables set in the parent script? I know this is mostly possible to do by wrapping the include in a function, but that feels sloppy.

Is there a more elegant alternative?

like image 561
Nathanael Avatar asked Nov 04 '22 20:11

Nathanael


2 Answers

Wrap the include command into a include function.

    <?php
    //Führt die Cronjobs aus
    set_time_limit(30);

    /** Alle Dateien mit _cronjob am schluss sollen minütlich ausgeführt werden.*/
    $files = scandir(__DIR__ ."/cronjob");

    foreach($files as $file) {
        if($file == "." || $file == "..") continue;
        include_easy(__DIR__ ."/cronjob/". $file);
    }

    //Damit die variablen nicht überschrieben werden, wird das include über eine funktion gemacht.
    function include_easy($pfad) {
        include($pfad);
    }
like image 173
Konstantin XFlash Stratigenas Avatar answered Nov 09 '22 14:11

Konstantin XFlash Stratigenas


Unfortunately, at the time of this writing, it appears that wrapping what you want to include in a function or method is the only way to achieve this.

like image 24
Nathanael Avatar answered Nov 09 '22 13:11

Nathanael