You add these lines once. The rest of the request your autoload-function will be used to load the class-files. You should put this somewhere at the start of your php, for example in the index.php. But if you have multiple entry-points (for example http://www.example.com/index.php and http://www.example.com/page.php) all these files should have the spl_autoload_register
(either via copy-paste but more preferable via an include(..)
).
Using spl_autoload_register
-
03-07-2022 - |
Question
Must I put the following lines in each PHP file I create to use autoloader? Or there is a solution to do it once, inside "index.php" of the application for example, and instantiate the classes in other files like usual?
function autoload($class) {
require('/path/to/mylibrary/' . str_replace('_', '/', $class) . '.php');
}
spl_autoload_register('autoload');
Thanks.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow