include, require inside functions methods

Asked

Viewed 2,583 times

12

Use include (or require) within a function or method may cause some conflict or problem with older versions of PHP (such as 5.3)?

I noticed that most frameworks working with MVC use require within a method, for example the file ./CodeIgniter/system/core/Loader.php codeigniter3:

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 believe that all PHP Frameworks based on routes (and mvc) work like this, so maybe it’s something that doesn’t cause problems in the latest versions of PHP, but I’d like to know about version 5.3 if there might be a problem (I won’t use version 5.3 is just curiosity).

My concern is due to require usually include a class (coming from the Model or Controller generally) at the time of execution and in the behavior of the API (PHP).

  • I believe there are no compatibility problems, the reason frameworks use this way is to follow standards rules of projects like PSR0, PSR1 and etc... See more here, click code styles tab.

  • 2

    A couple of years ago, I was making a budget for a client who had a php programmer who was "knows everything". Then the guy saw an excerpt of my codes and said it was garbage. He explained that you should never include/require within a function or class. But he didn’t explain why and I never found relevant information on the internet. I think he was talking nonsense. He just argued that it is bad for performance, that all includes and requires had to be done in procedural mode, out of functions or class methods. But I never found anything about it. rsrsr

  • @Danielomine friend I think one day I heard the same thing (will be the same person), but the problem was related to an old version of PHP, but I searched throughout CHANGELOG. I’m beginning to think that this was a myth, like other PHP ones, that the problem was one (include inside a function works with the variables of this function), ie only changes the behavior, Someone got it wrong and the brains of these guys supplemented it with lies because they didn’t know the real reason. I think that I saw a situation in PHP4, which may be the reason, this guy must have taken old information.

3 answers

10

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

This answer can be found on the PHP website in the include statement documentation: http://php.net/manual/en/function.include.php.

If the inclusion occurs within a function all the code contained in the included file will behave as if it had been defined within the function. Therefore it will follow the scope of function variables. An exception to this rule are the magic constants that are interpreted before 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
  • There are problems that can cause an error in the application, not to mention that it is bad practice, and the error quoted in question is when you are working with classes.

4

It doesn’t cause problems. At least I know PHP 5.0 up and never had problems with it.

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

Perils

One thing you might want to avoid is that a file that is included within a function have access to functions like func_get_args and the arguments of the function itself.

Look at the following example:

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

In the archive alguma_coisa.php

$user = null

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

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

Look at 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’t generalize. For example, autoload functions in PHP usually use include internally. And this is widely used in framework such as Laravel and Codeigniter. Even the Poser himself makes use of it.

Then see the example of an autoload implementation:

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

Classes of View (of the same MVC standard) usually uses a include to "render" a particular PHP layout.

public function render(array $data)
{

    ob_start();
    extract($data);

    include $this->file

    return ob_get_clean();
}

#Ficaadica

-3

As of version 5.0 there are no problems, as commented above, but it is a bad practice and you are importing a whole file into your code snippet, and if you are working with classes you will have problems with instances.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.