include_once / require_once Wrapper Class optimized

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;
    }
}
?>

2 comments

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

Datenschutz | Impressum