4

Possible Duplicate:
Why is require_once so bad to use?

When PHP is including a file I assume it stores the file name inside a array.

Then, when including another file, it will check that array to see if it's already included, right?

So what's the big deal here? Why are people so scared of this array check? It's not like you're including milions of files...

in_array checks are made all the time. I use them in almost all functions :)

Community
  • 1
  • 1
Amanda
  • 51
  • 1
  • 3
  • 5
    I'm not sure that performance is even relevant, since the two functions do different things. You should pick which one to used based on its functionality, not its performance. – cdhowie Sep 06 '11 at 16:01
  • 1
    You're right in saying that it's only a microscopic optimization, however I'm not convinced that "people are so scared of this array check" – Matt Sep 06 '11 at 16:01
  • Never heard that. Can you point out somewhere where it is said ? – Benjamin Crouzier Sep 06 '11 at 16:02
  • People tend to say a lot of nonsense, you know. – Your Common Sense Sep 06 '11 at 16:02
  • If you distribute your code across several files, it might actually be more efficient to use `require_once` consistently for files that contain library code, so no file has to be executed twice. – Niklas B. Sep 06 '11 at 16:03
  • 3
    There is extra **MICROSCOPIC** overhead to use the _once version v.s. plain require(), because of that array check. The people who complain it's slower are the ones who do code micro-optimization while still in the cocktail napkin design phase. – Marc B Sep 06 '11 at 16:05
  • Not that MICROSCOPIC. Opcode caches can't cache require_once and include_once (or not as efficiently). That's actually HUGE. Do benchmarks in production mode. – Arnaud Le Blanc Sep 06 '11 at 16:08

3 Answers3

6

I am not certain who the "people" are that say this, but I believe it to be one of many micro-optimization myths that pervade any language.

Here is an article which benchmarks the methods: http://arin.me/blog/php-require-vs-include-vs-require_once-vs-include_once-performance-test

Your mileage may vary, but I doubt you will see any significant performance gains by avoiding x_once functions. Use the language constructs that are right for the situation and you aren't doing anything wrong. x_once might be a sign that you need to reconsider your project's organization or consider using autoloader, but it isn't eval...

Chris Baker
  • 49,926
  • 12
  • 96
  • 115
2

require_once() has two issues, if you ignore the performance issue the second one is also important. If you are using require_once() it implies you may require the same file more than once. This is inherently wrong or faulty design. Now the performance part, for web an optimised app will do nothing. If you understand that serving a static HTML file or cached content is faster than serving a PHP file you will understand why people say using require_once() is slower.

Kumar
  • 5,038
  • 7
  • 39
  • 51
-3

As a matter of fact, for the sensible planned application, functions like require_once are useless.

For the slowness - it's negligible.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 2
    What do you mean they are useless? How would the Zend Framework, for instance, avoid using `require_once`? – moteutsch Sep 06 '11 at 16:06
  • Well, ZF just is *not sensible planned application*. – Your Common Sense Sep 06 '11 at 16:12
  • +1, reasonable planning is the key, tot you -1 me once today :) – ajreal Sep 06 '11 at 16:17
  • Please explain this. I think what you are saying is ridiculous. How, then, can you use two components that both require a third component? If they both use `require` you will get an error and if `require` is used once by default then there is a needless overhead if you choose not to use those components. – moteutsch Sep 06 '11 at 16:18
  • 2
    For classes, you can define an appropriate autoloader and name your class files sensibly, and they'll be loaded -- once -- when the class needs to be defined. – cHao Sep 06 '11 at 19:08