Answer the question
In order to leave comments, you need to log in
Performance. Will the dynamic file upload function eat up a lot of resources?
Is it advisable to use dynamic file loading.
There is a small class that implements the autolod function, in the future I also want to load any necessary file. How I load it: first, the function takes the path of the site root, then recursively builds an array with the paths of all directories, and then it runs through it, checks if the requested file exists, and if so, then connects it, and the forich is not interrupted until all the paths have been passed .
The code:
class Loader{
const DS = DIRECTORY_SEPARATOR;
private static $path;
private static $namespaces = false;
private static $root;
private static $dirlist=[];
public static function setRoot($root){
static::$root=$root;
}
public static function searchDir($root){
$scan = scandir($root);
foreach($scan as $dir){
if(is_dir($root.self::DS.$dir) and $dir != '.' and $dir !='..'){
static::$dirlist[]=$root.self::DS.$dir;
static::searchDir($root.self::DS.$dir);
}
}
}
public static function setAutoload($autoload=[self,'autoload']){
spl_autoload_register($autoload);
}
public static function autoload($class){
$class = explode('\\',strtolower($class));
$name = array_pop($class);
if(!empty($class)){
static::$path = static::$root.self::DS.implode(self::DS,$class).self::DS.$name.'.php';
static::$namespaces=true;
require_once(static::$path);
}
else{
static::$namespaces=false;
!empty(static::$dirlist)?:static::searchDir(static::$root);
foreach(static::$dirlist as $dir){
if(file_exists($dir.self::DS.$name.'.php')){
require_once($dir.self::DS.$name.'.php');
}
}
static::$path = static::$root.self::DS.$name.'.php';
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question