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

前端 未结 5 845
时光取名叫无心
时光取名叫无心 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条回答
  •  -上瘾入骨i
    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.

提交回复
热议问题