Skip to content

Instantly share code, notes, and snippets.

@dgrammatiko
Forked from iggyvolz/myclass.php
Created August 15, 2019 11:33
Show Gist options
  • Save dgrammatiko/cb111e241f705a8d10565ec14f2c2050 to your computer and use it in GitHub Desktop.
Save dgrammatiko/cb111e241f705a8d10565ec14f2c2050 to your computer and use it in GitHub Desktop.
Demo of PHP Preload
<?php
echo "myclass.php required".PHP_EOL;
class MyClass
{
public function MyFunction():void
{
echo "This is a function!".PHP_EOL;
}
}
<?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();
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!
[opcache]
; Still need to enable extension so we don't get error
zend_extension=opcache.so
opcache.enable=0
opcache.enable_cli=0
[opcache]
opcache.enable=1
zend_extension=opcache.so
opcache.enable_cli=1
[opcache]
opcache.enable=1
zend_extension=opcache.so
opcache.enable_cli=1
opcache.preload=myclass.php
<?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