There is a page on Preloading in the manual which explicitly mentions this scenario:
Additionally, preloading is only useful when there is a persistent process from one request to another. That means while it can work in a CLI script if the opcache is enabled, it's generally pointless.
To enable it, you would set something like this in your php.ini:
opcache.enable_cli=1
opcache.file_cache=/some/dir/somewhere
opcache.file_cache_only=1
opcache.preload=/path/to/preload.php
If you configure this and run a CLI script, the preload script in /path/to/preload.php
will be executed, and any use of include, include_once, require, require_once, or opcache_compile_file() will cause compiled opcodes to be stored in /some/dir/somewhere
.
When you run a second CLI script, it will be able to use those compiled opcodes, but it won't know that preloading has already happened. As such, everything that you put in preload.php will run on every script load anyway. You would need to handle this manually, e.g. by calling opcache_is_script_cached.
The complexity of using it all this way probably exceeds the gain for most use cases.