[#]: subject: "Use autoloading and namespaces in PHP" [#]: via: "https://opensource.com/article/23/4/autoloading-namespaces-php" [#]: author: "Jonathan Daggerhart https://opensource.com/users/daggerhart" [#]: collector: "lkxed" [#]: translator: " " [#]: reviewer: " " [#]: publisher: " " [#]: url: " " Use autoloading and namespaces in PHP ====== In the PHP language, autoloading is a way to automatically include class files of a project in your code. Say you had a complex object-oriented PHP project with more than a hundred PHP classes. You'd need to ensure all your classes were loaded before using them. This article aims to help you understand the what, why, and how of autoloading, along with namespaces and the `use` keyword, in PHP. ### What is autoloading? In a complex PHP project, you're probably using hundreds of classes. Without autoloading, you'd likely have to include every class manually. Your code would look like this: ``` /Jonathan/SomeBundle/Validator.php`. Just to drive this point home, here are more examples of where a PHP file exists for a class within a project making use of PSR-4: - **File location**: `/Project/Fields/Email/Validator.php` - **File location**: `/Acme/QueryBuilder/Where.php` - **File location**: `/MyFirstProject/Entity/EventEmitter.php` - **Namespace and class**: `\Project\Fields\Email\Validator()` - **Namespace and class**: `\Acme\QueryBuilder\Where` - **Namespace and class**: `\MyFirstProject\Entity\EventEmitter` This isn't actually 100% accurate. Each component of a project has its own relative root, but don't discount this information: Knowing that PSR-4 implies the file location of a class helps you easily find any class within a large project. ### How does PSR-4 work? PSR-4 works because it's achieved with an autoloader function. Take a look at one PSR-4 example autoloader function: ``` /src/Foo/Bar/Baz/Bug.php.` - If the file is found, load it. In other words, you change `Foo\Bar\Baz\Bug` to `/src/Foo/Bar/Baz/Bug.php` then locate that file. ### Composer and autoloading [Composer][1] is a command-line PHP package manager. You may have seen a project with a `composer.json` file in its root directory. This file tells Composer about the project, including the project's dependencies. Here's an example of a simple `composer.json` file: ``` { "name": "jonathan/example", "description": "This is an example composer.json file", "require": { "twig/twig": "^1.24" } } ``` This project is named "jonathan/example" and has one dependency: the Twig templating engine (at version 1.24 or higher). With Composer installed, you can use the JSON file to download the project's dependencies. In doing so, Composer generates an `autoload.php` file that automatically handles autoloading the classes in all of your dependencies. ![Screenshot of nested drop down menus highlighting the path example - vendor - twig - autoload.php][2] If you include this new file in a project, all classes within your dependency are automatically loaded, as needed. ### PSR makes PHP better Because of the PSR-4 standard and its widespread adoption, Composer can generate an autoloader that automatically handles loading your dependencies as you instantiate them within your project. The next time you write PHP code, keep namespaces and autoloading in mind. -------------------------------------------------------------------------------- via: https://opensource.com/article/23/4/autoloading-namespaces-php 作者:[Jonathan Daggerhart][a] 选题:[lkxed][b] 译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 [a]: https://opensource.com/users/daggerhart [b]: https://github.com/lkxed/ [1]: https://opensource.com/article/22/5/composer-git-repositories [2]: https://opensource.com/sites/default/files/2023-03/composer_autoloading_php_menu.png