PHP Performance on including multiple files

2019-07-19 02:28发布

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.

标签: php include
6条回答
Viruses.
2楼-- · 2019-07-19 02:48

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

查看更多
劫难
3楼-- · 2019-07-19 02:52

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.

查看更多
混吃等死
4楼-- · 2019-07-19 02:54

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楼-- · 2019-07-19 03:03

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.

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-07-19 03:08

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.

查看更多
Viruses.
7楼-- · 2019-07-19 03:14

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
查看更多
登录 后发表回答