Question

Question très simple Cette fois-ci, fondamentalement, j'ai un groupe de dossiers et certains d'entre eux contiennent des fichiers que j'aimerais autoload chaque fois que j'exécute mon script de sites.Cependant, je ne veux pas avoir à spécifier quels fichiers à l'automated car je souhaite que le processus soit dynamique et que je souhaite pouvoir créer et supprimer différents fichiers à la volée. Donc, bien sûr, la solution la plus facile consiste à obtenir une liste de dossiers dans les répertoires et à créer les chemins des fichiers de chargement automatique, si les fichiers existent, le script les inclut. Mais ma question est de savoir combien va-t-il affecter la performance de mon script?C'est en fait un cadre que je veux libérer plus tard sur la performance est tout à fait le problème. Des idées?

Était-ce utile?

La solution

You should consider simply letting PHP autoload your classes.

If that won't work, then you're pretty much left with the directory-scanning solution, and you shouldn't really care about performance penalties. If you want the functionality, you'll put up with the costs.

Generally you shouldn't stress overmuch about performance in PHP anyways. If it becomes an issue when your framework is complete, revisit it. Chances are you'll find whatever performance gains/losses you incur now are rendered moot by implementing a good caching system in your framework.

See Premature Optimization.

Autres conseils

It depends on your disk speed, file system, and the size of the directory. No matter what, having to dynamically load the directory to get the list of files will take longer than loading a static list; how much of an increase is acceptable depends on your requirements.

If it's an option, caching that list could help.

Autoloading is great although it's not "free", the performance hit is not noticeable. Of course you can measure this and refactor if needed.

Here's my autoloader:

spl_autoload_register(
    function ($className) {
        $possibilities = array(
            APPLICATION_PATH.'beans'.DIRECTORY_SEPARATOR.$className.'.php',
            APPLICATION_PATH.'controllers'.DIRECTORY_SEPARATOR.$className.'.php',
            APPLICATION_PATH.'helpers'.DIRECTORY_SEPARATOR.$className.'.php',
            APPLICATION_PATH.'models'.DIRECTORY_SEPARATOR.$className.'.php',
            APPLICATION_PATH.'views'.DIRECTORY_SEPARATOR.$className.'.php'
        );
        foreach (explode(PATH_SEPARATOR, ini_get('include_path')) as $k => $v) {
            $possibilities[] = $v.DIRECTORY_SEPARATOR.$className.'.php';
        }
        foreach ($possibilities as $file) {
            if (file_exists($file)) {
                require_once($file);
                return true;
            }
        }
        return false;
    }
);

It depends.

Try your approach and measure. You can always add caching later. Or resort to autoload.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top