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' (www.akbkhome.com/blog...);
You can also use this wrapper class for include_once and require_once, it is faster.
<?php
class includeWrapper{
private static $paths = array();
public static function includeOnce($path_file){
if(!in_array($path_file,self::$paths)){
include($path_file);
self::$paths[] = $path_file;
}
}
public static function requireOnce($path_file){
if(!in_array($path_file,self::$paths)){
require($path_file);
self::$paths[] = $path_file;
}
}
}
includeWrapper::includeOnce("Class1.class.php");
includeWrapper::requireOnce("Class1.class.php");
includeWrapper::includeOnce("Class2.class.php");
?>
This is nice and all, but adding some real-world benchmarks would be much more helpful than just stating "this is faster".
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.
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.
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
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.
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.
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.
These methods won't work if you include the file from different relative path.
Ex:
directory structure:
/web
file1.php
include_me.php
/dir1
file2.php
but in file2.php it's done by
includeWrapper::includeOnce(dirname(__FILE__)."/../include_me.php");
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.
Joel
Interesting... I wrote something similar back in the day, but this is kinda similar.
-Ryan