Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more

Does PHP execute files from an include in a file I compile with opcache.opcache_compile_file()?

Ask Question

If I compile a file using opcache_compile_file() , and that file includes other files (via include() , require() , etc.), will the files that are included be executed? Or will the included files just be compiled and added to the opcache?

From a point raised in a comment, are the included files also added to the cache? Or does opcache_compile_file() simply ignore includes (maybe the optimal behavior)?

It wouldn't make sense to execute include files if you're not executing the file that includes them. Barmar Jun 22 '19 at 1:43 The real question should be whether the contents of the included files are included in the cache, or it has to execute those files when it later executes the cached file. Barmar Jun 22 '19 at 1:44 @Barmar I would agree, and that's what I'm hoping is the case. But it may not be fact. And your second comment is a really good question too - I'm added it to my top question. calligraphic-io Jun 22 '19 at 1:44 Try it and see? Put file_put_contents("/path/to/file.txt", "it ran it"); in an include file, then compile the file that includes it, and see if the file is written. Barmar Jun 22 '19 at 1:48 You can also use opcache_is_script_cached() to find out if the include file was compiled. Barmar Jun 22 '19 at 1:48

having the same question ,i googleize and i got some results by turning on opcache in xampp with stackoverflow fellows

so i did my succefull stupid test ..

in php.ini i put these

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

and so in htdocs\stupidtest*index.php* i put this:

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

in test.php i put

function test(){ echo 'yep';

and another file called the_answer_i_need.php

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

Now let's play the game: run first index.php http://localhost/test/index.php in my case

then i run the_answer_i_need.php http://localhost/test/the_answer_i_need.php in my case

the result i got is

1 means boolean true
'yep' means the function called are included from cache cause the previous result was **true**
*job done,we're good* 

i wrote the fastest php framework in the wold since 2002 and this trick will allow me to switch some parts of framework's core as 2nd pre procesor after the php and to gain around 40% more speed.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.