Search code examples
phpmysqlwordpressperformancepagespeed

is using " require_once " affect the speed of the website


I am working on a framework to make my workflow faster like a starter theme plus boilerplate. so one of main parts that will make the work faster is the plurality ...

I for example in function.php when I need to create new post type instead of writing a big block of code (130 line) for each new custom post type ... I moved the code to separated folder then I call it by

require_once( 'library/custom-post-type.php' );

in this way, I can have many ready custom post types as files and for each project, I can just add the file also for future maintenance instead of the scroll in very big 1000+ function file ... it will be a short file with a list of require_once and folder that has all the code in separated files

MY QUESTION:

  • Does the require_once !! affect the speed of the website?
  • when I should use it and when I should not
  • whats the harm of overusing it to get a more arranged code?

the Question is more like PHP / server related than WordPress.

EDIT 1 : example lets say I have this file

CASE 1

1 file

function.php

// lots of code 

// 130 line of code to create custom post 1 

// 130 line of code to create custom post 2

// 130 line of code to create custom post 3

// 130 line of code to create custom post 4

// 130 line of code to create custom post 5 

// lots of code 

CASE 2

total 6 files with the same amount of code + 5 lines of require_once

function.php

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

custom-post-type1.php

// 130 line of code to create custom post 1 

custom-post-type2.php

// 130 line of code to create custom post 2

custom-post-type3.php

// 130 line of code to create custom post 3 

custom-post-type4.php

// 130 line of code to create custom post 4 

custom-post-type5.php

// 130 line of code to create custom post 5 

why I prefer the case to is smaller files easy to find and edit and can move any file to other project and just call it

I want to create some modules structure ... but can not harm the speed


Solution

  • ANSWERS:

    1. Does the require_once !! affect the speed of the website?
      • no
    2. when I should use it and when I should not
    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.