문제

매우 간단한 질문 이번에는 기본적으로 폴더 그룹이 있으며 사이트 스크립트를 실행할 때마다 Autoload를 원하는 파일이 포함되어 있습니다.그러나 프로세스가 동적이되기를 원하기 때문에 AutoLoad 할 파일을 지정해야하지 않으므로 파리에서 다른 파일을 만들고 삭제할 수 있기 때문입니다. 물론 가장 쉬운 솔루션은 디렉토리의 폴더 목록을 가져 와서 자동로드 파일에 대한 경로를 빌드하는 것입니다. 파일이있는 경우 스크립트가 포함됩니다. 그러나 내 질문은 내 스크립트의 성과에 영향을 미치는 것입니다.실제로 실제로 실적이 나중에 방출하려는 프레임 워크는 성능이 훨씬 문제입니다. 어떤 아이디어도 있습니까?

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top