简体   繁体   English

php处理包含文件

[英]php processing of include files

This question has to do with the internal mechanics of the PHP engine. 这个问题与PHP引擎的内部机制有关。 I'm asking it in an effort to understand the file include process. 我要求它努力理解文件包含过程。

Say, you got a 20,000 lines include file ( something like "functions_library.php" ) that gets included on all your php scripts. 比如,你有一个20,000行包含文件(类似“functions_library.php”),它包含在你所有的PHP脚本中。

Does PHP check/verify if that include file is syntactically correct every single time one of your php scripts load that file? 当你的一个php脚本加载该文件时,PHP是否检查/验证该包含文件在语法上是否正确? Does this process happen at each page load over and over and over again? 这个过程是否会在每次页面加载时反复发生?

Or... 要么...

Does PHP pay attention to the file's last modification date? PHP是否注意文件的最后修改日期? If it turns out that there were no changes to it since the last check, does it simply ignore the checking? 如果事实证明自上次检查以来没有对它进行任何更改,它是否只是忽略了检查?

On a default installation, the file will be parsed every single time. 在默认安装中,将每次解析该文件。 However, any production installation of PHP is recommended to have a bytecode cache, such as APC or many others . 但是,建议PHP的任何生产安装都具有字节码缓存,例如APC许多其他缓存。 When bytecode cache is used, the script is parsed the first time and the interpreted code will be stored in memory. 使用字节码缓存时,首次解析脚本,解释的代码将存储在内存中。

Different configurations may alter how often file modifications are checked. 不同的配置可能会改变检查文件修改的频率。 Under some configurations, for very high performance, manual flushing or restarting the web server may be required. 在某些配置下,为了获得非常高的性能,可能需要手动刷新或重新启动Web服务器。

如果包含该文件,PHP将需要每次都解释它。

PHP will re-interpret the file every time you call include() . 每次调用include()时,PHP都会重新解释该文件。

This can be tested visually with the use of variables. 这可以使用变量在视觉上进行测试。 If you have something like this: 如果您有这样的事情:

otherScript.php:
   <?php echo $foo; ?>

script.php:
    <?php
    $foo = 1;
    include("otherScript.php");
    $foo = 2;
    include("otherScript.php");
    ?>

The output will be: 输出将是:

12

This is a poor example, but it should demonstrate the PHP will need to include the contents of the file each time. 这是一个糟糕的例子,但它应该证明PHP每次都需要包含文件的内容。 It is a pretty good guess to state that the PHP interpreter won't keep a copy of the included file in memory for each reference - there can be hundreds of includes per application. 很好的猜测是,PHP解释器不会为每个引用保留内存中包含文件的副本 - 每个应用程序可能有数百个包含。

To deal with this specific ideal, however, they provide a method include_once() that will, as the name implies, only include the file one time and prevent it from being included any additional times. 然而,为了处理这个特定的理想,它们提供了一个方法include_once() ,顾名思义,它只包含一次文件,并防止它被包含在任何额外的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM