doufu7835 2014-06-26 17:16
浏览 72
已采纳

如何在php中包含文件后排除文件?

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?

  • 写回答

3条回答 默认 最新

  • dongqin1819 2014-06-26 17:28
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计