A
A
alestro2015-11-18 17:51:26
PHP
alestro, 2015-11-18 17:51:26

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

Regarding unnecessary gestures with setting the $namespaces flag - at first I wanted to break the code into functions, but I don’t know yet whether it is expedient at all, I don’t want to do it.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Muhammad, 2015-11-18
@muhammad_97

It's better to see how it's done in Composer . A class map (classmap) is built and saved to a file.

A
Adamos, 2015-11-18
@Adamos

Leave the bikes!

I
IsaevDev, 2016-05-16
@IsaevDev

Need to dig towards psr-4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question