Include file in PHP?

I am developing an application where I have a requirement that I have to include multiple files:

File1.php will include File2.php

File2.php will include File3.php

File3.php will include File4.php

      

Now what would be the best and most streamlined way to do this. I tried to include include (), but I think it slows down my server as the server load schedules are about 3-5 times the server load.

All of these files have a heave arround value 50 KB

for each file. So who can suggest me what would be the best option to include these files so that it doesn't affect the site speed,

What will be better?

require()

require_once()

include()

include_once()

      

OR if anyone has any other option please tell me

thanks

+1


a source to share


8 answers


You have to reorganize your files. Have a "common.php" file that contains all the necessary files. You shouldn't have such a chain.

I sincerely doubt that a couple of times are slowing down your server. Most web applications have dozens, maybe hundreds of PHP files. There is no way for 3 additional files to slow down your application.



Also, the speed difference between the features you mentioned is negligible, although you should use include_once

/ require_once

instead of your copies to make sure you don't include the same file multiple times.

+2


a source


The difference between include () and require () is whether you get a warning or a fatal error (respectively). The PHP docs don't mention the performance difference.

Using the _once () options is useful if you want to avoid duplicating the contents of included files. From the PHP docs for include_once:



include_once () can be used in cases where the same file can be included and evaluated more than once during a particular script execution, so in this case it can help to avoid problems such as overriding functions, overriding variable values, etc. ...

+3


a source


Explain what you need to know. In one of my projects, I had to implement some kind of factory for a lot of classes (about 50). I created a file for each class and they were very slow as well. I decided to join them in one file. This unbearable performance is very noticeable.

I think this is due to the slowdown of the file system when there are many entries ... I don't know why this is, but the fact is that including one large file is much faster than 50 small ones.

+2


a source


The Tech Your Universe article talks about the vs require_once requirement . In essence, when executing absolute paths, it is require()

definitely faster than require_once()

and include()

definitely faster than include_once()

. Their exact requirement when they were replacing one-time calls with sans-once calls:

First, strace found that there were 2848 system calls to serve the page, down from 4782, (-40%). Then I went to ab for more thorough testing and the average page request time dropped to 36.5, from 46.5ms (-22%), and the server was able to serve 27.1 requests per second, from 21.4 (+% 22).

According to Konstantin Rozinov's comment 90017 on the require_once page at php.net , the difference between require()

and is require_once()

tiny and only matters for large web applications. I personally would have liked the definition of big, but I think we got the idea. The reviewer also states that "when using APC code caching, the difference in speed between the two is completely irrelevant." Regarding the actual implementation of require and require_once: "This means that either the OS (Ubuntu Linux in my case) or Apache is" caching "or knows the results of previous stat () calls, so it doesn't stop them from repeating." Take this for what it's worth.

My personal summation:

  • If you are going to use require_once

    , try using absolute paths.
  • Be sure to use opcode caching.
+2


a source


Your answer will depend on your needs, mostly. Keep in mind:

  • include is faster than required.
  • If the file is "included" but not found, this does not result in a fatal error. The script will continue running.
  • If the file is "requried" but not found, the script is executed immediately.

However, a side note: if your application contains file4 in file3, file3 in file2, and file2 in file1, then I would say you should rethink your file organization. I'm sure you can find a better way to organize your code in files.

ADDITIONALLY: The speed difference between include and require is pretty minor. However, require_once () is significantly heavier than require (). Likewise for include_once () and include ().

amuses,

JRH

0


a source


I prefer to split everything into as many files as I feel comfortable developing, but then flatten it all into one file before uploading to the server.

PHP string and regular functions make it easy to write a script that will replace any include 'somefile.php'; with the actual content of this file (steaming <?> if necessary).

0


a source


Definitely select include()

or require()

by options. ' *_once()

' You probably need some kind of cache code to improve performance.

0


a source


I would suggest using the xdebug PHP extension for testing and profiling your script. It will tell you how long each function took. You can find more details on how to install it and what it can do here: http://www.xdebug.org/

0


a source







All Articles