PHP include semantics

孤人 提交于 2019-12-10 20:15:37

问题


I am trying to work out how to interpret PHP's include construct, e.g. whether it is textual inclusion, when it is evaluated etc. As usual, the documentation is rather informal and vague.

Based on experimentation, it seems to be syntactic sugar. Specifically, the construct

include 'Baz.php'

is an expression which can be replaced with

eval('?>' . file_get_contents('Baz.php',  FILE_USE_INCLUDE_PATH))

Is this correct? Is this substitution true in the general case, or just in my tests?

Edit: as bwoebi notes, this is not generally true since the evaluated code does not have the same __DIR__ and __FILE__ magic constants. If there were some way to set them, we could model that too.

Edit 2: this question is a duplicate of this one: Equivalent of include using eval. However, all the answers there appear to omit bwoebi's point about the file path context.


回答1:


Yes, that should be generally true.

The only differences are:

  • file context (it's now eval()'ed code, not code from that file)
  • relative paths: when you include files in other directories and they use theirselves relative paths, the file it points to might now not be found anymore
  • file_get_contents() is not affected by allow_url_include ini setting (though allow_url_fopen affect both)



回答2:


Other things to note are:

  • This will break PHP Opcode caching, as the file contents are read into memory as text, and then parsed/evaluated, producing a significant hit on performance if you are not expecting it. As mentioned in this question.
  • Parse errors in the code will not be fatal to the rest of the script, which has the side-effect of allowing the script execution to continue, even though logic that is expected to have happen may not have. (This may be true of other types of errors as well).
  • Error reporting will not resolve to the correct file/line, and therefore, debugging will become difficult.
  • This will not support the _once construct, which allows including/requiring files strictly once. This means duplicate class/function declarations will be fatal and/or behave unexpectedly.

I am certain there are other considerations that make this inadvisable as well. Without knowing your desired functionality or the reason you need to do this, giving you a helpful answer is difficult.

The best part of utilizing eval is the recovery from errors (if you could call this a "best" thing). If that is the benefit you are trying to take advantage of, however, this kind of is your only option.... Unfortunately...



来源:https://stackoverflow.com/questions/23681582/php-include-semantics

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