djmzfKnm
djmzfKnm

Reputation: 27185

Including file in PHP?

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

File1.php will include File2.php

File2.php will include File3.php

File3.php will include File4.php

Now, what will be the best and optimized way of doing these. I tried with include() but I think its slowing down my server as my server load graphs goes approx 3-5 times higher than normal server load.

These all files are heave means arround 50 KB for each file. So can anyone please suggest me what will be the best option to include these files so that it will not affect the speed of the site,

Which will be best?

require()

require_once()

include()

include_once()

OR if anyone has some other option then please tell me,

Thanks

Upvotes: 1

Views: 833

Answers (8)

Levi Morrison
Levi Morrison

Reputation: 19552

An article on Tech Your Universe talks about the require vs require_once issue. In essense, when doing absolute paths require() is definitely faster than require_once() and include() is definitely faster than include_once(). Their exact claim when they replaced the once calls with the sans-once calls:

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

According to comment 90017 by Konstantin Rozinov on the require_once page on php.net, the difference between require() and require_once() is tiny and only makes a difference on large web applications. I would personally like a definition of large, but I think we get the idea. The commentator also claims that "when using APC opcode caching, the speed difference between the two is completely irrelevant." Regarding the actual implementation of require and require_once: "This indicates that either the OS (Ubuntu Linux in my case), or Apache is "caching" or knows the results of the previous stat() calls, so it doesn't bother repeating them." Take that for what it is worth.

My personal summation:

  • If you are going to use require_once, try to use absolute paths.
  • Be sure to use opcode caching of some sort.

Upvotes: 2

Benjamin Ortuzar
Benjamin Ortuzar

Reputation: 7831

I would suggest using xdebug PHP extention to benchmark and profile your script. It will tell you how long each function took to execute. You can find more information on how to install it and what it can do here: http://www.xdebug.org/

Upvotes: 0

Jet
Jet

Reputation: 1171

Jusn a note to know. In one of my projects I had to implement a kind of factory for lots of classes (about 50). I created a file for each class and including them was dramatically slow. I decided to join them into one file. This incereased performance very appreciable.

I think it is related to file system slowdowns when there are many entries... Not sure why it's so, but fact - include of 1 big file much faster then 50 small.

Upvotes: 2

jrharshath
jrharshath

Reputation: 26583

Your answer will depend on your need, mostly. Points to keep in mind are:

  1. include is faster than require.
  2. if a file is "included" but is not found, it does not result in a fatal error. The script will continue running.
  3. if a file is "requried" but is not found, the script execution stops instantly.

However, a side note: if your application is including file4 in file3, file3 in file2 and file2 in file1, then I'd say you should reconsider your file organisation. I'm sure you can come up with a better way of organising your code into files.

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

cheers,

jrh

Upvotes: 0

too much php
too much php

Reputation: 91018

Definitely choose include() or require() over the '*_once()' variants. You probably need some sort of opcode cache to improve your performance.

Upvotes: 0

Ilya Birman
Ilya Birman

Reputation: 10072

I prefer splitting everything into as many files as it’s convenient for me to develop, but then flatten it all into one file before uploading it to server.

PHP string and regex functions make it super easy to write a script that will replace any include 'somefile.php'; with actual contents of that file (stripping < ? ? > if necessary).

Upvotes: 0

Sasha Chedygov
Sasha Chedygov

Reputation: 130817

You should reorganize your files. Have a "common.php" file that includes all the needed files. You shouldn't have a chain like that.

I sincerely doubt a couple of includes are slowing down your server. Most web applications have tens, maybe even hundreds of PHP files. There is no way 3 extra files will slow down your application that much.

Also, the speed difference between the functions you mentioned is negligible, although you should use include_once/require_once instead of their counterparts, to make sure you don't include the same file multiple times.

Upvotes: 2

Jimmy
Jimmy

Reputation: 28386

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

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

include_once() may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so in this case it may help avoid problems such as function redefinitions, variable value reassignments, etc.

Upvotes: 3

Related Questions