You can optimize by eliminating the technique that iterates through an array of directories.
$cDir = array('pasta1', 'pasta2', 'pasta3')
foreach ($cDir as $dirName) {
// aqui vai repetir, buscando nos diretórios registrados até encontrar.
// isso é redundante e pode ser evitado
}
One suggestion is to standardize on recommendations from php-fig.org .
You must have seen something like PHP PSR, PSR4, etc. somewhere.
The PSR stands for > PHP Standards Recommendation.
It is not a rule that everyone should follow. It is optional.
Optimizing with PSR4
To optimize your autoload, you could do something in the PSR4 pattern:
spl_autoload_register(function ($class) {
// project-specific namespace prefix
$prefix = 'Foo\Bar\';
// base directory for the namespace prefix
$base_dir = __DIR__ . '/src/';
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relative_class = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});
It's just that. The only thing you would change is the prefix of your namespace and the basis of the files.
$prefix = 'Foo\Bar\';
If your namespace is Qualquer\Outro\Nome
, it would look like this
$prefix = 'Qualquer\Outro\Nome\';
The base of the files is defined in this section
$base_dir = __DIR__ . '/src/';
Just set up the base according to your project.
Obviously you will have to use namespace in your files that you want to load.
The naming of directories and files should follow the same pattern as the class name.
Example, when instantiating a class:
new \Foo\Bar\Baz\Qux;
The autoload of the above example will recognize the excerpt of the namespace prefix: \Foo\Bar
and com will work with the rest Baz\Qux
Forming this way, the location of the file is
/www/site/src/Baz/Qux.php
This logic eliminates all the cost of iterating an array to go through directories that are most often unnecessary and still run the risk of colliding with an equal name into different directories.
This array technique was useful for a time when PHP still did not have the namespaces feature. a>, available from PHP5.3.
Related to the namespace: How do namespaces work in PHP?