Domanda

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.

È stato utile?

Soluzione

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(..)).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top