include_once Wrapper Class

Submitted by pure-php on Thu, 2005-03-17 23:17.

There hase been some discussion in the recend days about the performance of include_once. Inlude_once is really slower than include. For the inclusion of the classes you can use
class_exists('PEAR') or require_once 'PEAR.php' (;

You can also use this wrapper class for include_once and require_once, it is faster.

class includeWrapper{
    private static 
$paths = array();
    public static function 
self::$paths[] = $path_file;
    public static function 
self::$paths[] = $path_file;

add new comment
Submitted by Ryan Brooks (not verified) on Fri, 2005-03-18 19:45.

Interesting... I wrote something similar back in the day, but this is kinda similar.


Submitted by Markus wolff (not verified) on Sat, 2005-03-19 16:44.

This is nice and all, but adding some real-world benchmarks would be much more helpful than just stating "this is faster".

Submitted by Mathias Taylor (not verified) on Mon, 2005-03-21 01:29.

Agreed - I was curious myself so running "strace -c php -r '$replace_source_here'" revealed that this implementation does run a slight bit faster:

Class wrapper completed in 0.007032 seconds after 739 calls and 397 errors (not real errors). Using the respecive PHP core language functions it took 0.007919 seconds with 748 calls.

Submitted by Anonymous (not verified) on Mon, 2005-03-21 09:20.

Tank you for this benchmarking, include_once becomes slower with the number of you includes. So if you have many includes, and a complex app, it is slower.

Submitted by Cristian Strian (not verified) on Fri, 2005-03-25 16:05.

While the idea is nice, the implementation could use a little optimization.

Instead of simply putting the path in $paths and search for it later with in_array(), I'd rather use the built-in PHP hash tables since they are a lot faster for this job.

self::$paths[$path_file] = true; // store

isset(self::$paths[$path_file]); // query

Submitted by M (not verified) on Mon, 2005-03-28 09:26.

I wanted to add a bug I noticed (if it is a bug). When using variables declared outside the include page, but needed inside the included page, it kicks an undefined variable error.

Submitted by pure php (not verified) on Wed, 2005-03-30 09:43.

Yes. Your are right, this is a bug, but actually it is thought to be used for classes and you can use it also
for functions. You can declare a variable twice, but not a function, or a class.
For variables, you can use include.

Submitted by Ahhhh (not verified) on Sat, 2005-04-02 00:08.

I was under the impression that this include/require class wrapper was a total replacement for the include/require *not really functions* built into php. But if used just for including classes then not a problem. But then I don't see the need for a wrapper for JUST classes? Why not for the whole shebang?

Great little script though.

Submitted by Joel Alexandre (not verified) on Fri, 2005-12-23 11:47.

These methods won't work if you include the file from different relative path.
directory structure:


in file1.php the include is done by:

but in file2.php it's done by

Using the includeWrapper class, the same file get's included twice.
I guess the php's include|require _once are slower because it translates the path.

If you'll add that missing feature, then it works ok.

If i'm wrong, let me know.


Submitted by pure-php on Tue, 2006-02-21 20:50.

You are right. It doesn't work with relative paths

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Datenschutz | Impressum