Why is require_once so bad to use?

Why is require_once so bad to use?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Performance Myth of require_once in PHP 5

You will find plenty of advice online telling you to avoid require_once because it is slow. This claim often stems from a misunderstanding of how the PHP opcode cache works versus how file inclusion actually executes. In PHP 5, specifically, the performance penalty associated with require_once is negligible in almost every real-world scenario, provided your application has an enabled opcode cache like APC or Xcache.

The confusion usually arises from comparing two different execution paths: one where a file is loaded multiple times within the same request versus one where it is loaded for the first time. If you use require_once, PHP checks if the file is already included in the current script. If it is, the code inside that file does not run again; only the inclusion mechanism itself incurs a tiny overhead. If the file isn't there, PHP reads the file from disk, parses the syntax, and executes the code.

The critical factor here is persistence. Once a file is parsed and compiled into opcodes by your opcode cache, subsequent requests load those opcodes directly from memory. The cost of reading the file from disk happens only once per deployment or cache clear. Whether you used require_once, include_once, or even standard require for that initial load makes zero difference to the speed of subsequent requests. The overhead of checking "have we seen this before" is so minimal it is dwarfed by the cost of function calls, database queries, and network I/O happening in your application logic.

If you are running PHP 5 without an opcode cache, then yes, every file inclusion adds to the total request time because the parser has to work harder. However, skipping require_once doesn't magically make you faster; it just removes the safety net that prevents duplicate code execution within a single request.

The Real Problem: Duplicate Code Execution

The primary reason developers should prefer include_once or require_once over plain include or require is not speed; it is correctness. Without the _once suffix, if you accidentally include a file multiple times in your script, the code inside that file runs multiple times.

Consider a configuration loader:

// config.php
function loadSettings() {
    $db_host = 'localhost';
    // ... database connection setup ...
}

If you have this logic spread across several files and you require the same file twice in your main entry point, the database connection might be initialized twice. While modern frameworks handle this gracefully, duplicate code execution can lead to unexpected behavior, memory leaks, or errors if a function is not idempotent.

The _once keyword ensures that the inclusion happens exactly one time per script execution. This is a logical guarantee, not a performance optimization. In PHP 5, relying on this guarantee is good practice regardless of the minor CPU cycles spent checking the global include stack.

Modern Alternatives in Laravel

If you are looking for ways to manage dependencies more cleanly and avoid the friction of manual require statements, consider adopting a dependency injection container or using Composer autoloading. These approaches remove the need to manually include files scattered throughout your project.

For modern PHP development, specifically if you are moving beyond version 5, Laravel provides a robust solution for this exact problem. The framework's service container handles class instantiation and file inclusion automatically based on namespace definitions in your composer.json. This eliminates the need for manual require_once statements entirely, as the autoloader takes care of loading classes only when they are first encountered.

You can read more about how to structure your projects and manage dependencies effectively at https://laravelcompany.com. They provide resources on building scalable applications where you never have to worry about manual file inclusion performance bottlenecks.

Conclusion

Stop worrying about the speed of require_once. In PHP 5 with an opcode cache, it is fast enough. The only reason to use it is to prevent accidental duplicate code execution within a single request. Focus your optimization efforts on database queries, caching strategies, and reducing network latency instead of trying to shave milliseconds off file inclusion logic that rarely happens more than once per request.

Our network