How to remove an include file?

那年仲夏 提交于 2020-01-23 02:16:31

问题


I have two files that are included in my page. Like this:

// mypage.php
include 'script1.php';
include 'script2.php';

I need both of them in first, and then I need to remove one of them, something like this:

if ($var == 'one') {
      // inactive script1.php file
} else {
     // inactive script2.php file
}

That's hard to explain why I need to inactive one of them, I Just want to know, how can I do that? Is it possible to do unlike include?


回答1:


The simple answer is no, you can't.

The expanded answer is that when you run PHP, it makes two passes. The first pass compiles the PHP into machine code. This is where includes are added. The second pass is to execute the code from the first pass. Since the compilation pass is where the include was done, there is no way to remove it at runtime.

Since you're having a function collision, here's how to get around that using objects(classes)

class Bob {
    public static function samename($args) {

   }
}
class Fred {
   public static function samename($args) {

   }
}

Note that both classes have the samename() function but they live within a different class so there's no collision. Because they are static you can call them like so

Bob::samename($somearghere);
Fred::samename($somearghere);



回答2:


If you need just the output of either file you could do this

ob_start();
include('file1.php');
$file1 = ob_get_contents();

ob_start();
include('file2.php');
$file2 = ob_get_contents();

Then later if you need to call them

if ($var == 'one') {
    echo $file2;
} else {
    echo $file1;
}



回答3:


Your only option is something like this:

if ($var == 'one') {
      include('script2.php');
} else {
     include('script1.php');
}

You can't 'remove' code, you can only choose to not include/execute it in the first place.




回答4:


As by your comments you said its because of duplicated function names, i'm assuming you use both files elsewhere separately, but what your trying to achieve now is to merge these files together for a different reason (both files have functions/variables, etc that you need)

If your first file had a function like so my_function:

my_function() {
    // code here
}

and your second file also had the same named function you can wrap an if statement around it to exclude it:

if (!function_exists('my_function')) {
    my_function() {
        // code here
    }
}

This way the second file's function wont be available when merging the two files together but using file separately both functions will be available.




回答5:


For the sake of providing options for others that come here, some solutions I've used myself on occasion...

  1. If the files you're including, you're including for some function with a return value or some execution that doesn't need to be displayed on the page (such as mailing stuff out), and let's say you can't alter either of the target files (let's say they're somebody else's code or part of some highly integrated other piece of software that you really don't want to untangle).

A solution is to create a quick and dirty restful interface for both files to pull from them what you need from them, and then call that interface with your program, effectively bypassing the need to include them.

  1. A worse method but if your situation is truly desperate, and is truly the route of last resort, and will only work in some cases (for example, will break on namespacing)...

    $bad_idea = file_get_contents("foo.php");

    eval($bad_idea);

    unset($bad_idea);

Again, note, this is an option of last resort.



来源:https://stackoverflow.com/questions/31478294/how-to-remove-an-include-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!