DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Wrapper Class For Include_once / Require_once

05.14.2005
| 4690 views |
  • submit to reddit
        Here's a great wrapper class for include_once & require_once. Picked this one up from Pure PHP (http://www.pure-php.de/node/22).

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

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

<strong>Source Code:</strong>
<?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;
    }
}
?>