{"id":1863,"date":"2021-05-26T07:48:01","date_gmt":"2021-05-26T07:48:01","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=1863"},"modified":"2021-06-27T05:07:41","modified_gmt":"2021-06-27T05:07:41","slug":"php-composer-autoload","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-oop\/php-composer-autoload\/","title":{"rendered":"PHP Composer Autoload"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use Composer to autoload PHP classes from files using PSR-4 standard.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='loading-classes-using-the-require_once-construct'>Loading classes using the require_once construct <a href=\"#loading-classes-using-the-require_once-construct\" class=\"anchor\" id=\"loading-classes-using-the-require_once-construct\" title=\"Anchor for Loading classes using the require_once construct\">#<\/a><\/h2>\n\n\n\n<p>First, create the following directory structure with files:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">.\n\u251c\u2500\u2500 app\n\u2502   \u251c\u2500\u2500 bootstrap.php\n\u2502   \u2514\u2500\u2500 models\n\u2502       \u2514\u2500\u2500 User.php\n\u2514\u2500\u2500 index.php<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>User.php<\/code> file in the <code>models<\/code> folder holds the <code>User<\/code> <a href=\"https:\/\/phptutorial.net\/php-oop\/php-objects\/\">class<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">User<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">private<\/span> $username;\n\n    <span class=\"hljs-keyword\">private<\/span> $password;\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">($username, $password)<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">$this<\/span>-&gt;username = $username;\n        <span class=\"hljs-keyword\">$this<\/span>-&gt;password = password_hash($password);\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">getUsername<\/span><span class=\"hljs-params\">()<\/span>: <span class=\"hljs-title\">string<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">$this<\/span>-&gt;username;\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>User<\/code> is a simple class. It has two properties <code>$username<\/code> and <code>$password<\/code>. The <a href=\"https:\/\/phptutorial.net\/php-oop\/php-constructors\/\">constructor<\/a> initializes the properties from its arguments. Also, it uses the <code>password_hash()<\/code> function to hash the <code>$password<\/code>.<\/p>\n\n\n\n<p>The <code>bootstrap.php<\/code> file uses the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-require\/\">require_once<\/a><\/code> construct to load the <code>User<\/code> class from the <code>User.php<\/code> file in the <code>models<\/code> folder:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require_once<\/span> <span class=\"hljs-string\">'models\/User.php'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you have more classes in the <code>models<\/code> folder, you can add more <code>require_once<\/code> statement to the <code>bootstrap.php<\/code> file to load those classes.<\/p>\n\n\n\n<p>The <code>index.php<\/code> file loads the <code>bootstrap.php<\/code> file and uses the <code>User<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'.\/app\/bootstrap.php'<\/span>;\n\n$user = <span class=\"hljs-keyword\">new<\/span> User(<span class=\"hljs-string\">'admin'<\/span>, <span class=\"hljs-string\">'$ecurePa$$w0rd1'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This application structure works well if you have a small number of classes. However, when the application has a large number of classes, the <code>require_once<\/code> doesn&#8217;t scale well. In this case, you can use the <code><a href=\"https:\/\/phptutorial.net\/php-oop\/php-autoloading-class-files\/\">spl_autoload_register()<\/a><\/code> function to<a href=\"https:\/\/phptutorial.net\/php-oop\/php-autoloading-class-files\/\"> automatically loads the classes from their files<\/a>.<\/p>\n\n\n\n<p>The problem with the <code>spl_autoload_register()<\/code> function is that you have to implement the autoloader functions by yourself. And your autoloaders may not like autoloaders developed by other developers.<\/p>\n\n\n\n<p>Therefore, when you work with a different codebase, you need to study the autoloaders in that particular codebase to understand how they work.<\/p>\n\n\n\n<p>This is why Composer comes into play.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-composer'>Introduction to the Composer <a href=\"#introduction-to-the-composer\" class=\"anchor\" id=\"introduction-to-the-composer\" title=\"Anchor for Introduction to the Composer\">#<\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/getcomposer.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Composer <\/a>is a dependency manager for PHP. Composer allows you to manage dependencies in your PHP project. In this tutorial, we&#8217;ll focus on how to use the Composer for autoloading classes.<\/p>\n\n\n\n<p>Before using Composer, you need to download and install it. The official documentation provides you with the <a href=\"https:\/\/getcomposer.org\/download\/\" target=\"_blank\" rel=\"noreferrer noopener\">detailed steps of how to download and install Composer on your computer<\/a>. <\/p>\n\n\n\n<p>To check whether the Composer installed successfully, you run the following command from the Command Prompt on Windows or Terminal on macOS and Linux:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">composer -v<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It&#8217;ll return the current version and a lot of options that you can use with the <code>composer<\/code> command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='autoloading-classes-with-composer'>Autoloading classes with Composer <a href=\"#autoloading-classes-with-composer\" class=\"anchor\" id=\"autoloading-classes-with-composer\" title=\"Anchor for Autoloading classes with Composer\">#<\/a><\/h2>\n\n\n\n<p>Back the the previous example, to use the Composer, you first create a new file called <code>composer.json<\/code> under the project&#8217;s root folder. The project directory will look like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">.\n\u251c\u2500\u2500 app\n\u2502   \u251c\u2500\u2500 bootstrap.php\n\u2502   \u2514\u2500\u2500 models\n\u2502       \u2514\u2500\u2500 User.php\n\u251c\u2500\u2500 composer.json\n\u2514\u2500\u2500 index.php<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code>composer.json<\/code>, you add the following code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">{\n    <span class=\"hljs-string\">\"autoload\"<\/span>: {\n        <span class=\"hljs-string\">\"classmap\"<\/span>: &#91;<span class=\"hljs-string\">\"app\/models\"<\/span>]\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This code means that Composer will autoload all class files defined the <code>app\/models<\/code> folder.<\/p>\n\n\n\n<p>If you have classes from other folders that you want to load, you can specify them in <code>classmap<\/code> array:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">    {\n        <span class=\"hljs-string\">\"autoload\"<\/span>: {\n            <span class=\"hljs-string\">\"classmap\"<\/span>: &#91;<span class=\"hljs-string\">\"app\/models\"<\/span>, <span class=\"hljs-string\">\"app\/services\"<\/span>]\n        }\n    }<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, Composer will load classes from both <code>models<\/code> and <code>services<\/code> folders under the <code>app<\/code> folder.<\/p>\n\n\n\n<p>Next, launch the Command Prompt on Windows or Terminal on macOS and Linux, and navigate to the project directory.<\/p>\n\n\n\n<p>Then, type the following command from the project directory:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">composer dump-autoload<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Composer will generate a directory called <code>vendor<\/code> that contains a number of files like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">.\n\u251c\u2500\u2500 app\n\u2502   \u251c\u2500\u2500 bootstrap.php\n\u2502   \u2514\u2500\u2500 models\n\u2502       \u2514\u2500\u2500 User.php\n\u251c\u2500\u2500 composer.json\n\u251c\u2500\u2500 index.php\n\u2514\u2500\u2500 vendor\n    \u251c\u2500\u2500 autoload.php\n    \u2514\u2500\u2500 composer\n        \u251c\u2500\u2500 autoload_classmap.php\n        \u251c\u2500\u2500 autoload_namespaces.php\n        \u251c\u2500\u2500 autoload_psr4.php\n        \u251c\u2500\u2500 autoload_real.php\n        \u251c\u2500\u2500 autoload_static.php\n        \u251c\u2500\u2500 ClassLoader.php\n        \u2514\u2500\u2500 LICENSE<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The most important file to you for now is <code>autoload.php<\/code> file.<\/p>\n\n\n\n<p>After that, load the <code>autoload.php<\/code> file in the <code>bootstrap.php<\/code> file using the <code>require_once<\/code> construct:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require_once<\/span> <span class=\"hljs-keyword\">__DIR__<\/span> . <span class=\"hljs-string\">'\/..\/vendor\/autoload.php'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, you can use the <code>User<\/code> class in the <code>index.php<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'.\/app\/bootstrap.php'<\/span>;\n\n$user = <span class=\"hljs-keyword\">new<\/span> User(<span class=\"hljs-string\">'admin'<\/span>, <span class=\"hljs-string\">'$ecurePa$$w0rd1'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>From now, whenever you have a new class in the <code>models<\/code> directory, you need to run the command <code>composer dump-autoload<\/code> again to regenerate the <code>autoload.php<\/code> file.<\/p>\n\n\n\n<p>For example, the following defines a new class called <code>Comment<\/code> in the <code>Comment.php<\/code> file under the <code>models<\/code> folder:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Comment<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">private<\/span> $comment;\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">(string $comment)<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">$this<\/span>-&gt;comment = $comment;\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">getComment<\/span><span class=\"hljs-params\">()<\/span>: <span class=\"hljs-title\">string<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> strip_tags(<span class=\"hljs-keyword\">$this<\/span>-&gt;comment);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you don&#8217;t run the <code>composer dump-autoload<\/code> command and use the <code>Comment<\/code> class in the <code>index.php<\/code> file, you&#8217;ll get an error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'.\/app\/bootstrap.php'<\/span>;\n\n$user = <span class=\"hljs-keyword\">new<\/span> User(<span class=\"hljs-string\">'admin'<\/span>, <span class=\"hljs-string\">'$ecurePa$$w0rd1'<\/span>);\n\n$comment = <span class=\"hljs-keyword\">new<\/span> Comment(<span class=\"hljs-string\">'&lt;h1&gt;Hello&lt;\/h1&gt;'<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> $comment-&gt;getComment();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">Fatal error: Uncaught Error: <span class=\"hljs-class\"><span class=\"hljs-keyword\">Class<\/span> '<span class=\"hljs-title\">Comment<\/span>' <span class=\"hljs-title\">not<\/span> <span class=\"hljs-title\">found<\/span> <span class=\"hljs-title\">in<\/span>...<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, if you run the <code>composer dump-autoload<\/code> command again, the <code>index.php<\/code> file will work properly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='composer-autoload-with-psr-4'>Composer autoload with PSR-4 <a href=\"#composer-autoload-with-psr-4\" class=\"anchor\" id=\"composer-autoload-with-psr-4\" title=\"Anchor for Composer autoload with PSR-4\">#<\/a><\/h2>\n\n\n\n<p>PSR stands for PHP Standard Recommendation. PSR is a PHP specification published by the PHP Framework Interop Group or PHP-FIG.<\/p>\n\n\n\n<p>The goals of PSR are to enable interoperability of PHP components and to provide a common technical basis for the implementation of best practices in PHP programming.<\/p>\n\n\n\n<p>PHP-FIG has published a lot of PSR starting from PSR-0. <a href=\"https:\/\/www.php-fig.org\/psr\/\" target=\"_blank\" rel=\"noreferrer noopener\">For a complete list of PSR, check it out the PSR page<\/a>.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.php-fig.org\/psr\/psr-4\/\" target=\"_blank\" rel=\"noreferrer noopener\">PSR-4 is auto-loading standard<\/a> that describes the specification for autoloading classes from file paths. https:\/\/www.php-fig.org\/psr\/psr-4\/<\/p>\n\n\n\n<p>According to the PSR-4, a fully qualified class name has the following structure:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"> \\&lt;NamespaceName&gt;(\\&lt;SubNamespaceNames&gt;)*\\&lt;ClassName&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The structure starts with a namespace, followed by one or more sub namespaces, and the class name.<\/p>\n\n\n\n<p>To comply with PSR-4, you need to structure the previous application like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">.\n\u251c\u2500\u2500 app\n\u2502   \u251c\u2500\u2500 Acme\n\u2502   \u2502   \u251c\u2500\u2500 Auth\n\u2502   \u2502   \u2502   \u2514\u2500\u2500 User.php\n\u2502   \u2502   \u2514\u2500\u2500 Blog\n\u2502   \u2502       \u2514\u2500\u2500 Comment.php\n\u2502   \u2514\u2500\u2500 bootstrap.php\n\u251c\u2500\u2500 composer.json\n\u2514\u2500\u2500 index.php<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The new structure has the following changes:<\/p>\n\n\n\n<p>First, the <code>models<\/code> directory is deleted.<\/p>\n\n\n\n<p>Second, the <code>User.php<\/code> is under the <code>Acme\/Auth<\/code> folder. And the <code>User<\/code> class is namespaced with <code>Acme\/Auth<\/code>. Notice how namespaces map to the directory structure. This also helps you find a class file more quickly by looking at its namespace.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">Acme<\/span>\\<span class=\"hljs-title\">Auth<\/span>;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">User<\/span>\n<\/span>{\n    <span class=\"hljs-comment\">\/\/ implementation<\/span>\n    <span class=\"hljs-comment\">\/\/ ...<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, the <code>Comment.php<\/code> is under the <code>Acme\/Blog<\/code> folder. The <code>Comment<\/code> class has the namespace <code>Acme\\Blog<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">Acme<\/span>\\<span class=\"hljs-title\">Blog<\/span>;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Comment<\/span>\n<\/span>{\n    <span class=\"hljs-comment\">\/\/ implementation<\/span>\n    <span class=\"hljs-comment\">\/\/ ...<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, the <code>composer.json<\/code> file looks like the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">{\n    <span class=\"hljs-string\">\"autoload\"<\/span>: {\n        <span class=\"hljs-string\">\"psr-4\"<\/span>: {\n            <span class=\"hljs-string\">\"Acme\\\\\"<\/span>:<span class=\"hljs-string\">\"app\/Acme\"<\/span>\n        }\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Instead using the <code>classmap<\/code>, the <code>composer.json<\/code> file now uses <code>psr-4<\/code>. The <code>psr-4<\/code> maps the namespace <code>\"Acme\\\\\"<\/code> to the <code>\"app\/Acme\"<\/code> folder.<\/p>\n\n\n\n<p>Note that the second backslash (<code>\\<\/code>) in the <code>Acme\\<\/code> namespace is used to escape the first backslash (<code>\\<\/code>).<\/p>\n\n\n\n<p>Fifth, to use the <code>User<\/code> and <code>Comment<\/code> classes in the <code>index.php<\/code> file, you need to run the <code>composer dump-autoload<\/code> command to generate the autoload.php file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">composer dump-autoload<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since the <code>User<\/code> and <code>Comment<\/code> classes have namespaces, you need to have the <code>use<\/code> statements in <code>index.php<\/code> as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'.\/app\/bootstrap.php'<\/span>;\n\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Acme<\/span>\\<span class=\"hljs-title\">Auth<\/span>\\<span class=\"hljs-title\">User<\/span> <span class=\"hljs-title\">as<\/span> <span class=\"hljs-title\">User<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Acme<\/span>\\<span class=\"hljs-title\">Blog<\/span>\\<span class=\"hljs-title\">Comment<\/span> <span class=\"hljs-title\">as<\/span> <span class=\"hljs-title\">Comment<\/span>;\n\n$user = <span class=\"hljs-keyword\">new<\/span> User(<span class=\"hljs-string\">'admin'<\/span>, <span class=\"hljs-string\">'$ecurePa$$w0rd1'<\/span>);\n\n$comment = <span class=\"hljs-keyword\">new<\/span> Comment(<span class=\"hljs-string\">'&lt;h1&gt;Hello&lt;\/h1&gt;'<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> $comment-&gt;getComment();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Composer is a dependency management tool in PHP.<\/li><li>Use PSR-4 for organizing directory and class files.<\/li><li>Use the <code>composer dump-autoload<\/code> command to generate the <code>autoload.php<\/code> file.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1863\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-composer-autoload\/\"\n\t\t\t\tdata-post-title=\"PHP Composer Autoload\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1863\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-composer-autoload\/\"\n\t\t\t\tdata-post-title=\"PHP Composer Autoload\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use Composer to autoload PHP classes from files using PSR-4 standard.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1753,"menu_order":30,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1863","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=1863"}],"version-history":[{"count":2,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1863\/revisions"}],"predecessor-version":[{"id":2049,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1863\/revisions\/2049"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1753"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=1863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}