Reputation: 344
I have been searching around about autoloader and every solution I have been found was about the classes (SPL autoload), composer and so on.
So I was thinking, what about non-class files, where I have some functions, some procedural code, etc.
I know about this one method, but it's not a really good solution for me.
$RequiredFiles = array(
'/functions/function1.php',
'/functions/function2.php',
'/procedural/code1.php',
'/procedural/code2.php',
....
);
foreach ($RequiredFiles as $File) {
$Path = __DIR__ . $File;
require $Path;
}
My question is, how I can make an autoloader for non-class files?
I don't want to do something like this
foreach (glob('/functions/*.php') as $File) {
if( $File !== '/functions/donotincludethis.php'){
require $File;
}
}
I want to have some better and secure way to do that, of course without composer.
Upvotes: 2
Views: 1573
Reputation: 1
Automatic loading is to solve the dependencies that the code needs to run. I think of a method, which is similar to a container. The name of the dependency of the code you need to solve depends on the key of the array. The value of this array is the PHP file to be imported. The require_once
function is recommended.
Upvotes: -1
Reputation: 522005
Only classes and interfaces can be autoloaded. It doesn't make sense for "procedural code files" to be autoloaded; how would you even refer to them except with require 'file.php'
? Functions could theoretically be autoloaded but practically PHP doesn't allow it, so for including function definitions you'll still need to use require_once 'my_funcs.php'
at the top of your file.
Upvotes: 2