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

前端 未结 5 846
时光取名叫无心
时光取名叫无心 2021-01-05 23:32

Is the following example appropriate for PHP\'s require_once construct?

function foo( $param )
{
    require_once \"my_file.php\" ;
    //
    // do somethin         


        
相关标签:
5条回答
  • 2021-01-06 00:07

    When using require_once keep in mind that this is not some pre-processor directive. The require_once statements are executed when PHP runs the code and it only executes if the specific script has not already been included during the execution.

    For example:

    conf.php:

    <?php
    $maxAge = 40;
    ?>
    

    myscript.php

    <?php
     function foo($age) {
      require_once("conf.php");
      if($age > $maxAge) 
        return "1"; 
      else 
        return "0";
    }
    
     echo foo(30); // Echos 1
     echo foo(30); // Echos 0
    ?>
    

    The require_once is not executed on the second call to foo(..) since conf.php has already been included once.

    0 讨论(0)
  • 2021-01-06 00:11

    Maybe you only need the included file in certain cases, and you'd like to avoid including it if you don't need it at all, if it's a big file. So, I guess you could go for a require_once only in one branch of an if - else statement.

    0 讨论(0)
  • 2021-01-06 00:12

    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";
    }
    
    0 讨论(0)
  • 2021-01-06 00:15

    This is something of a religious debate.

    PROS for require and include statements at the top of the file:

    1. dependencies are clearly documented in a consistent reliable place.

    2. increased readability/maintainability

    3. OP code caching is simpler (although you could argue that this doesn't affect the developer directly)

    CONS for require and include statements at the top of the file:

    1. If you're doing some kind of dynamic runtime including (such as with __autoload()), a hardcoded statement at the top of the file is impossible.

    2. If only one execution path in the code uses an include, having it included every time, unconditionally is a waste of resources.

    3. long list of include or require statement is just noise the developer must scroll past when editing a file. Of course, a long list of dependencies can be viewed as a sign that the code should be broken up into smaller more focused pieces, so maybe you could spin this one as a PRO because it makes a code smell stand out.

    0 讨论(0)
  • 2021-01-06 00:16

    If you don't want to load a file unless it's needed, look into autoloading - on newer PHP via spl_autoload_register().

    0 讨论(0)
提交回复
热议问题