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.
– abfurlan
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
– Daniel Omine
@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.
– Guilherme Nascimento