include_once / require_once Wrapper Class optimized

Submitted by pure-php on Fri, 2005-03-25 22:38.

When I posted my Idea of building a wrapper class for include_once and require_once, I didn't think, that the many php developer would like it. But I was wrong, fortunately.

Thanks to Mathias Taylor for benchmarking. Special thanks goes to Cristian Strian for his suggestion to optimize the class, and make it faster. That is the optimized wrapper class.

Usage:

<?php
require("includeWrapper.class.php");
includeWrapper::includeOnce("test.php");
includeWrapper::includeOnce("test.php");
includeWrapper::includeOnce("test.php");
print_r(includeWrapper::getPaths());
?>

Output:
Array
(
[test.php] => 1
)

The class itself.

<?php
class includeWrapper{
    public static 
$paths = array();
    public static function 
includeOnce($path_file){
        if(!isset(
self::$paths[$path_file])){
            include(
$path_file);
            
self::$paths[$path_file] = true;
        }
    }
    public static function 
requireOnce($path_file){
        if(!isset(
self::$paths[$path_file])){
            require(
$path_file);
            
self::$paths[$path_file] = true;
        }
    }
    
// just for testing
    
public static function getPaths(){
        return 
self::$paths;
    }
}
?>

add new comment
Submitted by Gregory (not verified) on Wed, 2005-03-30 22:37.

I've just tried this hack out. It works but has problems passing vars
around.

Submitted by John B (not verified) on Fri, 2005-06-17 06:37.

Just curious what is the point of this? Sorry, I've only been cranking away at php for about a year, so alot is still new.

That code is php5 correct? public and static won't work in php4?

Comment viewing options

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