Why Is Require_Once So Bad to Use

Why is require_once so bad to use?

require_once and include_once both require that the system keeps a log of what's already been included/required. Every *_once call means checking that log. So there's definitely some extra work being done there but enough to detriment the speed of the whole app?

... I really doubt it... Not unless you're on really old hardware or doing it a lot.

If you are doing thousands of *_once, you could do the work yourself in a lighter fashion. For simple apps, just making sure you've only included it once should suffice but if you're still getting redefine errors, you could something like this:

if (!defined('MyIncludeName')) {
require('MyIncludeName');
define('MyIncludeName', 1);
}

I'll personally stick with the *_once statements but on silly million-pass benchmark, you can see a difference between the two:

                php                  hhvm
if defined 0.18587779998779 0.046600103378296
require_once 1.2219581604004 3.2908599376678

10-100× slower with require_once and it's curious that require_once is seemingly slower in hhvm. Again, this is only relevant to your code if you're running *_once thousands of times.


<?php // test.php

$LIMIT = 1000000;

$start = microtime(true);

for ($i=0; $i<$LIMIT; $i++)
if (!defined('include.php')) {
require('include.php');
define('include.php', 1);
}

$mid = microtime(true);

for ($i=0; $i<$LIMIT; $i++)
require_once('include.php');

$end = microtime(true);

printf("if defined\t%s\nrequire_once\t%s\n", $mid-$start, $end-$mid);

<?php // include.php

// do nothing.

is using require_once affect the speed of the website

ANSWERS:

  1. Does the require_once !! affect the speed of the website?

    • no
  2. when I should use it and when I should not

    • you should use it when you need it to be included once in the whole PHP script, read http://php.net/require_once
  3. whats the harm of overusing it to get a more arranged code?

    • it depends on the code that them contains, I mean that if the required file contains too much code that will be executed (not only function) the speed of the script that require it will be slowed down.

Warning: if the require_once fails, as per require, a fatal E_COMPILE_ERROR level error will be raised.

An example of file that may slow down your script when required may be:

<?php
if($condition)
{
// execute lots of code
}
else
{
// execute lots of code
}

// execute lots of code

// those does not slow down if not called
function f()
{
// ...
}
function f2()
{
// ...
}
function f3()
{
// ...
}
?>

The functions require_once(), require(), include_once() and include() simple do this: copy your content instead of the row when it is called.

Example:

If the file functions.php contains:

<?php
function f()
{
// ...
}
function f2()
{
// ...
}
function f3()
{
// ...
}
?>

The following file

<?php
require_once('functions.php');

// some code
?>

will be as this:

<?php
function f()
{
// ...
}
function f2()
{
// ...
}
function f3()
{
// ...
}

// some code
?>

Edit, about your CASEs

I suggest to you to do the combination of the two CASEs:

CASE 1+2

1 file function.php that itself require the 5 specific files:

// lots of code 

require_once( 'library/custom-post-type1.php' );
require_once( 'library/custom-post-type2.php' );
require_once( 'library/custom-post-type3.php' );
require_once( 'library/custom-post-type4.php' );
require_once( 'library/custom-post-type5.php' );

// lots of code

By doing that, you can include a file named functions.php that contains all the functions that all file can share together.

Inside this file, you can more compact the code by grouping all the similar functions in a separate file and require it inside it.

The result is that your code will be more readable and maintainable in prospect of future edits and more developer will be able to modify a single function's file simultaneously.

Why do people say require_once is slower than require?

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...

To require_once() or Not To require_once()

Earlier this year, I came upon this exact problem while developing a framework in PHP.

I considered the pros-cons and here's my evaluation:

Option 1 - Front Controller Pattern script include all other scripts

Advantages

  • Inclusion of packages are done within one script; you can see what files are included what are not at one glance.
  • Inclusion of a particular package is always called once, there is no overhead.

Disadvantages

  • Consider the case of such:

We have two classes Rectangle and Shape. Rectangle is a child class i.e. extension of Shape. However the core script includes the classes alphabetically. Thus when Rectangle is included, Shape is not found and PHP will throw an error.

Rectangle class:

class Rectangle extends Shape{

}

Shape class:

class Shape{

}
  • more overhead when everything that is not needed is also loaded into the memory.

Option 2 - Load main packages, then load other packages as-needed

Advantages

  • Files are only included when needed. Reduces overhead in another way
  • Solves the problem mentioned in Option 1.
  • You are able to concentrate on what each package requires from other packages and simply just load them

Disadvantages

  • Overhead as multiple requests for a particular package may occur.
  • Package inclusion is done in every single file.

Programming code is for human. Therefore to make things more logical and breaking down the problem, I chose option 2 to go for the framework.

Should require_once some file.php ; appear anywhere but the top of the file?

It comes down to a matter of coding style and opinion. Personally I keep all my require_once statements at the very top of my files so I can easily see which files are included where, nothing worse then some buried include messing with your scripts. However, if you have several large required scripts that are only required for certain functions, then putting the require_once inside a function would be OK from a performance stand-point, just make sure to put a note at the top of the page.

<?php
//require_once "my_file.php" (see function foo)

function foo($param) {
require_once "my_file.php";
}

Why require_once sometimes fails when used within a method?

I think this will be only included on the first execution of the method and not on the second one. The second execution of the method will then have the variables not defined. You should use require instead.

Is it worth using require_once() for memory savings?

I highly doubt you will gain anything from refactoring it into a separate file. Also, do you actually have any problems with the code as it is now that makes you consider this? You know premature optimization is the root of all evil. Profile your code to see if and where it has bottlenecks before you waste your time on pointless µoptimizations.

Is it better to use require_once('filename.php') or require_once 'filename.php';

It's exactly the same thing. It's a matter of style.

The parentheses may get in the way some times. For instance, this example from the manual doesn't do what you expect:

if (include('vars.php') == 'OK') {
echo 'OK';
}

See example #4.



Related Topics



Leave a reply



Submit