6

After we add a file inside Myfile.php with:

require_once 'abc.php';

or

include_once 'abc.php';

How we will remove the file? Or how to stop the abc.php file content being accessed after a certain block of code?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
user1638279
  • 523
  • 2
  • 12
  • 26
  • 2
    Depends on the content in the file. But in general once you load a file in via `require_once` or `include_once` it’s there and you cannot selectively turn it on or off. – Giacomo1968 Jun 26 '14 at 17:18
  • Why? There something wrong with your logic. – Kivylius Jun 26 '14 at 17:18
  • yes. if its cannot possible I have to spent some 2-3 hours to change my code. I have different files with a class inside them. but some class have same names. so in case i need to access 2 different files with same class, it will throw 'class redeclared error'. – user1638279 Jun 26 '14 at 17:22
  • Shouldn't have classes with the same name. If you can't avoid it (outside libraries) use name spaces: http://www.php.net/manual/en/language.namespaces.rationale.php – AbraCadaver Jun 26 '14 at 17:23
  • @AbraCadaver Ohh no.. I am actually using Gas ORM, which is creating table classes. so for table name `user_details` and `payment_details`, it will create classes in `user` folder and `payment` folder with `details.php` file and class - `details` .. – user1638279 Jun 26 '14 at 17:28
  • OK, and... those classes have different names. – AbraCadaver Jun 26 '14 at 17:30
  • @AbraCadaver name means namespaces ? yes different ofcourse. its table name itself like - `Model\user\details`, `Model\payment\details`. – user1638279 Jun 26 '14 at 17:32
  • I see. That's not a good way for the framework to do it. If there's not a way around it then switch. – AbraCadaver Jun 26 '14 at 17:50

3 Answers3

16

In PHP, once a resource is included, it can not be removed or "un-included". This is the very principle of PHP file inclusion. See : http://www.php.net/manual/en/function.include.php

The include statement includes and evaluates the specified file.

Once the interpreter has evaluated your code, the job is done. All operations have been taken into account, and in order to undo the changes, you have to perform the opposite operations. For instance, if your included file declares the class MyClass then you would need to undefine it, which is also impossible for very same reason as above. See : Unset Class.

If your file actually adds functions and not classes, then since PHP 5.3, you can use anonymous functions. This allows you to assign functions to variables, which can be unset. See this answer for details.

If a part of your code's logic has to disappear at some point, then you did not spend enough time designing before implementing.

If you need to undo an inclusion because of name conflicts, the problem is pretty much the same. However, a solution in this case would be to use namespaces. Still, a little review of your application design should be enough to avoid such conflicts.

Edit about frameworks : a single framework cannot fit for each and every application. Symfony, for instance, uses namespaces absolutely everywhere to avoid any possible conflicts (yet, some occur). If your framework does not offer you the possibility to easily distinguish two model classes with the same name, then I'd say it does not fit (at least, not with your design).

Community
  • 1
  • 1
John WH Smith
  • 2,743
  • 1
  • 21
  • 31
  • if you are trying to avoid unnecessary inclusion, you can enclose the include command within a conditional. Assuming $x != TRUE, the file "thisFile_inc.php" will not be loaded or parsed in the following: `if($x=TRUE){include("thisFile_inc.php);}`. – Parapluie Sep 25 '17 at 14:33
4

You can't. once its loaded cannot be removed.

joaofgf
  • 318
  • 1
  • 6
  • 10
    This is a comment. Not a answer. – Giacomo1968 Jun 26 '14 at 17:18
  • 3
    @ JakeGould the question is `how to...` the answer is `you can't`. I believe it is a direct answer to the primary question. – joaofgf Jun 26 '14 at 17:19
  • 4
    @JakeGould Actually, it is. What the OP is requesting is not possible, therefore the only correct answer is "you can't". Anything else would be about playing with semantics or workarounds. However, explaining *why* would be a good complement. – John WH Smith Jun 26 '14 at 17:23
  • @JohnWHSmith Again, this is a comment. Not a answer. It provides no details or explanation. Nor does it acknowledge the fact that from our perspective, none of us know what exactly is in these included files too begin with. As a comment it is valid. As an answer, it is not. – Giacomo1968 Jun 26 '14 at 17:40
0

There's a workaround. You can include the file while output buffering is turned on. Then save the output into a variable like $output. Use it if you need it, otherwise, just do unset($output) to discard it;

ob_start();
include('file.php'); 
$output = ob_get_contents();
ob_end_clean();

And later on, you can do unset($output) when you don't need it anymore.

Petr Fiedler
  • 278
  • 2
  • 10
arxoft
  • 1,385
  • 3
  • 17
  • 34