Search code examples
phpperformanceoptimizationpagespeed

PHP optimization request all functions on each page?


I have a php website and I want to optimize it. I have a config.php which requests all my files with all function on all pages (and I don't need to call that files for each page). Is it a bad thing? If I call for interface aly interface function and for admin just admin function, will I see any improvements?

require_once "inc/common.php";
//probabilnu se foloseste ^ jos
require_once "inc/producers_functions.php";

require_once "inc/slider_functions.php";
require_once "inc/static_pages_functions.php";
require_once "inc/products_functions.php";
require_once "inc/interface_functions.php";
require_once "inc/cart_functions.php";
require_once "inc/cart_product_functions.php";
require_once "inc/cart_option_functions.php";
require_once "inc/cart_product_cart_options_functions.php";
require_once "inc/session_functions.php";
require_once "inc/user_information_functions.php";
require_once "inc/invoice_functions.php";
require_once "inc/company_info_functions.php";
require_once "admin/assets/libs/phpmailer/class.phpmailer.php";
require_once "inc/promoted_products_functions.php";
require_once "inc/diagonal_widget_functions.php";
require_once "inc/round_widget_functions.php";
require_once "inc/order_functions.php";
require_once "inc/general_settings_functions.php";
require_once "inc/wishlist_functions.php";
require_once "inc/marquee_widget_functions.php";
require_once "inc/functions_adminac.php";
require_once "inc/show_category.php";
require_once "inc/function_login_reg.php";
require_once "inc/grey_widget_func.php";

Solution

  • Is it a bad thing? If I call for interface an interface function and for admin just admin function, will I see any improvements?

    The answer is "it depends" for both questions. If the files only declare new symbols and functions, there should be little difference. Even less if you use e.g. the opcache module which caches loaded files in memory.

    If the files (or some of them) perform some action that is time consuming, e.g. session management, database connection, logging etc., then yes, it can be a bad thing and changing it might be beneficial.

    There are several ways to optimize this kind of pattern, usually enclosing the code in classes and then using an "autoloader".

    You could declare specific symbols before including config.php so that this only includes certain files:

    <?php
        define('NEED_ADMIN', 1);
        define('NEED_MARQUEE', 1);
        include 'config.php';
    

    and in config,

    if (defined('NEED_ADMIN')) {
        ...
    }
    if (defined('NEED_PANEL')) {
        ...
    }
    

    You could also merge all files in a single file, or in several files. Then you would need some maintenance scripts that knows that, for example, "inc/block17.php" is the concatenation of inc/wishlist, inc/marquee and inc/login, and checks whether any of these files is newer than the master file; if it is, it reconcatenates all the components file into the master.

    $short = array(
        'inc/block1.php' => array(
             "inc/marquee_widget_functions.php",
             "inc/functions_adminac.php",
             "inc/show_category.php",
             "inc/function_login_reg.php",
             "inc/grey_widget_func.php",
        ),
        'inc/block2.php' => array(
             "inc/marquee_widget_functions.php",
             "inc/functions_adminac.php",
             "inc/diagonal_widget_functions.php",
        ),
    

    As you can see this approach leads to lots of code duplication inside the blocks* includes, but in some cases it could be worthwhile (as long as it's automatically handled and you don't need to do it by hand).

    There are also static analysis methods (not available for all app architectures) that can allow you to determine whether file1.php does indeed need a given include or not, thus allowing you to remove it.