include_once

You are currently browsing articles tagged include_once.

The problem:

If you include() or require() files with relative paths, APC will search the include_path on every request, and you won’t gain the full benefit of turning apc.stat off.

Using absolute paths portably in your own code is easy – just use a constant, eg. “require APP_PATH . ‘file.php’;”. External libraries, however, are harder to deal with – you either accept the performance hit, or go through them adding constants by hand (and merging your changes every time there’s a new release).

The solution:

This script rewrites PHP scripts, turning relative paths into absolute paths, allowing you to deploy third party libraries within your build process and still take full advantage of APC.

Some caveats:

It reads from stdin and writes to stdout.

It relies on it’s own include_path; in the real world, you’ll probably want to pass that in on the command line.

It only deals with the most common subset of include statements (variations on T_INCLUDE, T_CONSTANT_ENCAPSED_STRING, T_SEMICOLON).

The Windows support is a bit wing-and-a-prayer.

It’s memory-greedy when dealing with very large files.

It doesn’t play 100% nicely with safe_mode_include_dir.

Thrown out there in the hope it will be useful to someone. I’d be delighted to accept any feedback or additions anyone wants to offer.