phpshell-execmodular-designphp-parser

How to avoid php parsing the whole php file and includes and make it only parse what will be used?


I read somewhere that php parses the whole .php file every time it is executed. Some solution was proposed in there (that was not opcache), but I lost the website and I couldn't find it.

Now I have an enormous php website that has many long functions that are often used alone, and it's required that the execution be fast.

To avoid having php parsing all the other functions that won't be used, I was thinking of making a modular design in which the functions, stored in independent php files, will only be included if they will be actually used. But I haven't been able to confirm that php will not parse an include inside of a function or inside of a conditional statement unless it is required. Does php parse those includes?

Example:

<?php
    $func_to_execute = $_GET['func'];
    $parameter = $_GET['parameter'];
    switch($func_to_execute)
    {
        case 'a':
            include 'func_a.php';
            $output = func_a($parameter);
            break;
        case 'b':
            include 'func_b.php';
            $output = func_b($parameter);
            break;
        case 'c':
            include 'func_c.php';
            $output = func_c($parameter);
            break;
    };
    echo $output;
?>

In this example, I would like php to parse only the func_a if I am requesting a, only the func_b if I am requesting b, etcetera. There are in practice more than just 3 functions, and each is a very long algorythm with also very long strings and arrays.

As an alternative to includes I was thinking of making independent php files and execute them and retrieve their output only if they are required, with a shell_exec. But that would take other complexities, like formatting the parameters (I don't have idea of how I would pass a very long string with special characters, or a JSON, as a parameter in the shell) and calling the function to execute in the shell. Would those complexities make it slower than just letting php parse the whole file?

I know about the opcache function. Would it be enough even if all the ops of all the functions will be tested each time?

Are there other ways to make a PHP website modular, and not having php parsing the whole of php files everytime?

Thank you.


Solution

  • I made a benchmarking experiment and it seems that php truly does not parse conditional includes. I made the test using the example script mentioned, and defining each as:

    I measured the execution time from another php script with the function shell_exec(). The results were (in seconds):

    Therefore I conclude that: - Includes in a switch statement are not parsed unless they are actually required. - A syntax mistake in an include (inside a switch statement) will not launch any error if it is not actually required, because it is not parsed. - Anyway, the difference on the time of the process is very little (about 0.03 extra second for an extra text of 3.3 MB; or crudely said, 0.01 extra second per 1 MB of text to parse). However this might be important to consider if there are many users requesting the website at the same time, and therefore it might be useful to divide in modules (includes) if the script is actually that big. Also the fact that a wrongly written include that was not required be not parsed helps to not launch errors when they aren't relevant.

    It seems then for me a good manner to design a modular application in PHP where the modules be extremely big.