include, require within functions functions

12

Use include (or #

I noticed that most frameworks that work with MVC use require within a method, for example the CodeIgniter3 require file:

public function model($model, $name = '', $db_conn = FALSE)
{
    ...

    foreach ($this->_ci_model_paths as $mod_path)
    {
        if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
        {
            continue;
        }

        require_once($mod_path.'models/'.$path.$model.'.php');

        $this->_ci_models[] = $name;
        $CI->$name = new $model();
        return $this;
    }

    show_error('Unable to locate the model you have specified: '.$model);
}

I think all FrameWorks PHP-based routes (and mvc) work like this, so maybe it's something that does not cause problems in the latest versions of PHP, but I'd like to know about version 5.3 if there's going to be a problem using version 5.3 is just curious).

My concern is because the ./CodeIgniter/system/core/Loader.php usually includes a class (coming from the Model or Controller usually ) at runtime and behavior of the API (PHP).

    
asked by anonymous 27.04.2015 / 20:09

3 answers

10

There is no problem in doing includes within functions, what changes is only the scope. If you do include within the function, the scope of include is only within that function, ie it would be as if that included code was typed inside the function.

This response can be found on the PHP website in the documentation for the include statement: link .

  

If the inclusion occurs within a function all the code contained in the included file will behave as if it were defined within the function. Therefore it will follow the scope of function variables. An exception to this rule is the magic constants that are interpreted before the inclusion occurs.

// Object.php
<?php
return new Object();
?>

// Classe.php
<?php
class Classe {
    public static function createObject(){
         include_once 'Object.php';
    }
}

// file.php
<?php
$object = Classe::createObject();
var_dump($object instanceof Object); //true
    
27.04.2015 / 22:07
4

Does not cause problems. At least I know PHP 5.0 up and I've never had a problem with it.

include/require within functions can be done normally. The difference is that it will get "stuck" within the scope of that function.

Dangers

One thing you might want to avoid is that a file that is included inside a function has access to functions such as func_get_args and the function's own arguments.

Examine the following example:

  function add_user($user, $id)
  {
       include 'alguma_coisa.php';
  }

In the file alguma_coisa.php

$user = null

Imagine what will happen when the behavior of the function add_user ?

Another problem of giving include within a method of a class (not to be confused with function) is that you have direct access to $this , self and static , and you can also access private and protected properties .

See this question I asked here, where I also give a solution to the problem:

Include within class and access to $ this, self or static

Useful cases

Of course you can not generalize. For example, autoload functions in PHP generally use include internally. And this is widely used in framework as Laravel and Codeigniter . Even the composer himself makes use of it.

Then look at the example of an autoload implementation:

spl_autoload_register(function ($class)
{
      return include DIRETORIO_CLASSE . $class . '.php';
});

Classes of View (the one of the same MVC standard) usually uses a include to "render" certain layout done in PHP.

public function render(array $data)
{

    ob_start();
    extract($data);

    include $this->file

    return ob_get_clean();
}

#Abstract

    
18.12.2015 / 14:33
-3

From version 5.0 there are no problems, as commented above, however it is a bad practice and you are importing an entire file into your code snippet, and if you are working with classes you will have problems with instances

    
22.02.2017 / 15:33