How to remove an include file?

12,592

Solution 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);

Solution 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;
}
Share:
12,592
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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?