Last active
August 15, 2019 11:33
-
-
Save iggyvolz/572720ca82b4815556d15012efb79e6a to your computer and use it in GitHub Desktop.
Demo of PHP Preload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo "myclass.php required".PHP_EOL; | |
class MyClass | |
{ | |
public function MyFunction():void | |
{ | |
echo "This is a function!".PHP_EOL; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo "Start of myfile.php".PHP_EOL; | |
echo "Opcache is ".(opcache_get_status()?"on, files will be cached":"off, files will not be cached").PHP_EOL; | |
spl_autoload_register(function(){ | |
echo "Calling autoloader".PHP_EOL; | |
require_once "myclass.php"; | |
}); | |
$thing=new MyClass(); | |
$thing->MyFunction(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nothing: | |
Start of myfile.php | |
Opcache is off, files will not be cached | |
Calling autoloader | |
myclass.php required | |
This is a function! | |
opcache: | |
Start of myfile.php | |
Opcache is on, files will be cached | |
Calling autoloader | |
myclass.php required | |
This is a function! | |
preload: | |
myclass.php required | |
Start of myfile.php | |
Opcache is on, files will be cached | |
This is a function! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[opcache] | |
; Still need to enable extension so we don't get error | |
zend_extension=opcache.so | |
opcache.enable=0 | |
opcache.enable_cli=0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[opcache] | |
opcache.enable=1 | |
zend_extension=opcache.so | |
opcache.enable_cli=1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[opcache] | |
opcache.enable=1 | |
zend_extension=opcache.so | |
opcache.enable_cli=1 | |
opcache.preload=myclass.php |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$configs=["nothing","opcache","preload"]; | |
foreach($configs as $config) | |
{ | |
echo "$config:".PHP_EOL; | |
passthru("php -c php-$config.ini myfile.php"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with php run.php
In summary - php runs the file specified by opcache.preload on startup (typically server startup, but in this case CLI startup), takes all symbols (functions/classes) defined by it and make them accessible to any invocation of PHP (typically webpage load).
In this example, with no opcache loaded, the loading precedes as normal - when the class is constructed, PHP calls the autoloader, it loads the file from disk, compiles the file, and runs it to get the class. With opcache loaded, this procedure remains the same, but the file may be loaded from memory instead of disk if it has already been loaded. With preload, however, the class is loaded when PHP starts, so the autoloader is never called. (Note that typically you probably wouldn't want to specify the preload file to point directly to a class, but to some sort of autoloader for your program that will include all the classes you want).