Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include php files only when needed at runtime

Tags:

php

I have some PHP code which looks roughly like this

require_once 'cache.php';
require_once 'generator.php';

if (cache_dirty ('foo')) {
    cache_update ('foo', generate_foo ());
}

print cache_read ('foo');

My problem is that generator.php includes a whole mass of libraries and I don't want to load/parse it unless cache_dirty actually returns false at runtime.

I know there are PHP precompilers which can help but for now I need a quick fix. Is this possible?

like image 368
spraff Avatar asked Dec 07 '22 16:12

spraff


1 Answers

require_once 'cache.php';

if (cache_dirty ('foo')) {
    require_once 'generator.php';
    cache_update ('foo', generate_foo ());
}

print cache_read ('foo');

Fits your question quite well ...

like image 64
Eugen Rieck Avatar answered Dec 10 '22 11:12

Eugen Rieck