🐘 PHP Intermediate

What is PHP's Autoloading?

Answer

Autoloading automatically loads class files when they are first used, eliminating the need for manual require or include statements. Register a custom autoloader with spl_autoload_register(function($class) { include str_replace("\\", "/", $class) . ".php"; });. In modern PHP, Composer's autoloader handles this automatically based on PSR-4 configuration in composer.json. PSR-4 maps namespaces to directories: "autoload": { "psr-4": { "App\\": "src/" } }. After running composer dump-autoload, including vendor/autoload.php makes all your classes available without any manual includes. This is how all modern PHP frameworks and packages work.