0

Possible Duplicate:
cost of “include” in PHP?

This question is specifically about PHP, but I'm pretty sure that he answer, in nature, is similar for most modern programming languages that support classes (at least interpreted ones).

When you include or use a class (using namespaces for PHP >= 5.3.0), how much processing resources are spent?

For example, imagine there's a big PHP script file Database.php that has over a couple thousand lines of code, and you import it in another script:

# PHP >= 5.3.0
use Service\Database;

or

# in previous versions
include Service/Database.php;

I'm unsure of what happens behind the curtain in this case. Does it load all the class into working memory or just the functions you actually get to use? If you import it but don't use any of it, does it still waste resources?

Community
  • 1
  • 1
federico-t
  • 12,014
  • 19
  • 67
  • 111
  • 1
    If you think you're trying to optimize your code, stop. Break out [xdebug](http://xdebug.org/) or [xhprof](https://github.com/facebook/xhprof) and do *real* profiling. Use those tools to identify *real* performance problems in your code. If you think that file includes are hurting you, worry about it only once you can prove it. Until then, don't worry about it. (Psst, bytecode caching, look it up.) – Charles Dec 10 '12 at 08:40

1 Answers1

1

This question is specific to PHP, because other languages have completely different workings in there parsers.

Second comment: use does not include anything, so it is not a replacement for include/require.

Third comment: Because including the PHP source code into a script requires the code to be parsed into opcodes that is then interpreted, there are opcode caches that eliminate this step after the first time. These greatly improve performance.

What happens? Including a file executes that file. Which means that the source is parsed or fetched from a cache, then executed. Every line of code that defines something, like class or function, will define its portion for later lines of code. Every line that does something, like defining variables or echo, is executed as well and acts as intended.

Include is not a source code inclusion that is then parsed. Because the code is executed, not pasted as source, you cannot use include everywhere. In fact, include can act as a function itself, if the included code returns a value. This is commonly used to include configuration values, to benefit from the opcode cache instead of parsing INI files all the time:

config.php
return array('db' => 'example.com', 'user' => 'foo',);

other file:
$config = include('config.php);
Sven
  • 69,403
  • 10
  • 107
  • 109
  • I didn't know about using includes as a function on itself, that's pretty cool. But if the `use` directives don't actually include anything, what _do_ they do? Do they load the class into memory or it just creates an alias to refer to that class? – federico-t Dec 11 '12 at 02:17
  • They import classes and interfaces from other namespaces into yours. – Sven Dec 11 '12 at 20:27