phpinclude

How to remove an include file?


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?


Solution

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