PHP Performance on including multiple files

試著忘記壹切 提交于 2019-12-12 19:15:46

问题


My current workflow I include function and class files as and when I need to. However, this can get quite messy with you have quite a lot of them in which some depend on others.

So I'm considering using a head file which includes on the files in the includes directory. But my question is, are there any PHP performance issues for doing this over including as an when i need. Often times I have to use include_once, so doing 1 big include would get rid of the need for this.


回答1:


The best approach would probably be autoloading. You do not need to (manually) include any class at all, then. Take a look at this. I recommend using the spl_autoload_register()-function. That would resolve dependencies on the fly. The performance of includes is really irrelevant in most cases. The slow things usually happen in other places. Using autoloading has the added benefit of lazy loading. You do not load source files that are not in use. This might even speed up your application.




回答2:


PHP code is interpreted on the fly. If a given piece of code is not used, it will not be 'compiled' and so will not incur a performance hit.

However, all code on a given page (and its includes) goes through a syntax check so that may slow things down a little.

Certainly I would consider the includes that you have and whether or not you really need them.




回答3:


Normally performance (speed) in PHP is not affected by the amount of codelines or files but of:

  • Access to db
  • Access to file system!!!
  • Access to third party APIs (SOAP...)
  • Coding style



回答4:


I would recommend you look at autoloading: manual. I would also recommend using spl_autoload_register over one __autoload() function as it allows for greater control with separating out modules or namespaces.




回答5:


Well including files does have a hit on the performance of your app because it needs to read your app from the disk but if you stay below about 100 files this is trivial.

Btw if you don't like having to include your class files every time check out the magic method autoload:

function __autoload($class_name) {
    include $class_name . '.php';
}

http://php.net/manual/en/language.oop5.autoload.php




回答6:


There is a performance effect but it is not a very significant one. Do whatever makes it quicker and easier for you to write the code. If down the line you find that you really need that 1ms back, and you have already trimmed all of the other fat elsewhere, then go for it. Otherwise you are throwing away development time on trying to be "perfect" when it never actually makes a practical difference.



来源:https://stackoverflow.com/questions/6140391/php-performance-on-including-multiple-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!