Eval or include?

    I am developing one of the current projects on my own framework, running it in parallel and adding it. Why did I need to reinvent the wheel, and how does it differ from the existing ones, I will write when I will introduce it to the public. Now I want to share some thoughts about performance and at the same time listen to the opinions of colleagues. Perhaps my observations will be useful to those who do not use frameworks.

    When I had to implement a comment tree, I was faced with the need to recursively call a view (view in MVC). Since my views, and almost everywhere, are ordinary files with pieces of HTML code and the ability to insert PHP, they are connected using include. I felt uneasy when I imagined how this include is called recursively hundreds of times. The first thought is to put the view file on the first request into memory and then execute it through eval. This approach will allow you to cache the view code, and even store it in the database. Since eval eats only PHP, and our views are not pure PHP code, we frame the contents in '?>' And '<? Php'.

    So, to the point.

    Tests

    To begin with, in a variety of ways, in a loop, we display a simple view 'view.php' containing, for example: i=<?=$i?>

    CodeTime , s
    (in brackets - with eAccelerator)
    for ($ i = 0; $ i <100; $ i ++) include ('view.php');
    0.0058141 (0.002068)
    for ($ i = 0; $ i <100; $ i ++) {$ code = '?>'. file_get_contents ('view.php'). '<? php'; eval ($ code); }
    0.005527 (0.0056472)
    $ code = '?>'. file_get_contents ('view.php'). '<? php'; for ($ i = 0; $ i <100; $ i ++) {eval ($ code); }
    0.0015929 (0.0016122)


    And recursively

    CodeTime , s
    (in brackets - with eAccelerator)
    $ i = 0; include ('view.php'); ---------- view.php ---------- html <? php if (++ $ i <100) {include ('view.php'); }?>
    0.006865 (0.0019491)
    $ i = 0; $ code = '?>'. file_get_contents ('view.php'). '<? php'; eval ($ code); ---------- view.php ---------- html <? php if (++ $ i <100) {$ code = '?>'. file_get_contents ('view. php ').' <? php '; eval ($ code); }?>
    0.008599 (0.0087898)
    $ i = 0; $ code = '?>'. file_get_contents ('view.php'). '<? php'; eval ($ code); ---------- view.php ---------- html <? php if (++ $ i <100) {eval ($ code); }?>
    0.0034332 (0.0032461)


    findings

    The first thing that catches your eye - the options with eval are not amenable to optimization and caching using eAccelerator. Therefore, if you use it and you do not need other advantages of eval - it is better to focus on include.

    Using eval makes sense where views are called multiple times, but there is no way to use accelerators. Or if the views are stored in the database.

    The error handling in eval is also slightly different. If an error occurs parsing the code, for example, the execution of the script is not interrupted. This may be useful in some cases. But at the same time, the error message looks different, which can be a little confusing:

    Parse error: syntax error, unexpected T_ECHO, expecting ')' in /www/test/eval_vs_include/test.php(39) : eval()'d code on line 4

    Here, the indicated error occurred in line 4 of the file view.php, which was fed to eval, located in line 39 of the file test.php. Of course, nothing prevents us from displaying the name of the included file if eval returned false. Considering that work on errors is not a regular mode, and errors are not displayed on a working draft, I consider this drawback not significant.

    Eval

    + Faster than include if accelerators are not used
    + Ability to cache the view code in a framework or template engine
    + Ability to store views in the database
    + Script execution is not interrupted when errors occur
    - It is not optimized or cached by external accelerators
    - Error output differs from the usual
    - On some hosting services execution of eval may be prohibited

    Include

    + It works everywhere
    + External accelerators increase productivity by a lot
    - Low speed without accelerators
    - There is no way to cache the code included in the file in the engine
    - The connected code can only be in the file

    It should be noted that in the CodeIgniter framework eval is also used to display views, but only in the event that the settings indicate the need to replace the short '<? =' with '<? php esho' and at the same time short tags will be disabled in the PHP settings. In all other cases, include is used. CakePHP always uses include.

    Also popular now: