首页 > 解决方案 > PHP 是否从我使用 opcache.opcache_compile_file() 编译的文件中的包含执行文件?

问题描述

[PHP 文档opcache.opcache_compile_file说:

此函数编译 PHP 脚本并将其添加到操作码缓存中而不执行它。

如果我使用 编译一个文件opcache_compile_file(),并且该文件包含其他文件(通过include()require()等),是否会执行包含的文件?或者包含的文件是否会被编译并添加到 opcache 中?

编辑

从评论中提出的观点来看,包含的文件是否也添加到了缓存中?还是opcache_compile_file()只是忽略包含(也许是最佳行为)?

标签: phpopcache

解决方案


有同样的问题,我用谷歌搜索,我通过在 xampp 中与 stackoverflow 研究员打开 opcache 得到了一些结果

所以我做了我成功的愚蠢测试..

在 php.ini 我把这些

[opcache]
zend_extension=C:\xampp\php\ext\php_opcache.dll
opcache.enable=1
opcache.enable_cli=1

所以在 htdocs\stupidtest* index.php * 我把这个:

<?php
opcache_compile_file('test.php');
test();

test.php我放

<?php
function test(){
    echo 'yep';
}

和另一个名为the_answer_i_need.php 的文件

<?php
include('test.php');

print_r(opcache_is_script_cached('test.php'));  

test();
?>  

现在让我们玩游戏:在我的情况下首先运行 index.php http://localhost/test/index.php

然后我运行 the_answer_i_need.php http://localhost/test/the_answer_i_need.php 在我的情况下

我得到的结果是 1yep

1 means boolean true
'yep' means the function called are included from cache cause the previous result was **true**

*job done,we're good* 

自 2002 年以来,我编写了世界上最快的 php 框架,这个技巧将允许我将框架核心的某些部分切换为 php 之后的第二个预处理器,并获得大约 40% 的速度。


推荐阅读