Web Development Blog

24K Design Studio :: Blog :: Execute PHP's "require" or "include" only when necessary.
Execute PHP's "require" or "include" only when necessary.
03:03, 29 Jun, 2011
by David

As I learned the existence of __halt_compiler, I tried to compact huge data into rarely used PHP file.
And I use the file in this way:

if (...statement of init matched...) {
require('init.dat'); // It is *.dat since it contains binary data for __halt_complier

However, because require or include will include the file no matter the code is actually executed or not, the large amount of data will be processed.

After a few days' thinking, I finally came out with this idea which will execute require only when necessary:

if (...statement of init matched...) {

Note that eval('?>'.file_get_contents('init.dat')); will NOT do because of the existence of __halt_compiler

Tags: PHP
Your name:
Only visible to the author.

Tell us you are not a robot. What is captcha1captchaOcaptcha2 =

View the latest 5 entries.