{"id":5532,"date":"2018-02-08T13:00:38","date_gmt":"2018-02-08T04:00:38","guid":{"rendered":"https:\/\/tadtadya.com\/?p=5532"},"modified":"2021-01-25T14:15:52","modified_gmt":"2021-01-25T05:15:52","slug":"php-composer-lets-make-effective-use-of-autoload-function","status":"publish","type":"post","link":"https:\/\/tadtadya.com\/en\/php-composer-lets-make-effective-use-of-autoload-function\/","title":{"rendered":"PHP composer, Let's make the most of autoload !"},"content":{"rendered":"\n<p>PHP autoloading is the ability to define a 'use' operator to automatically load a class. It does not use the 'require' operator.<\/p>\n\n\n\n<p>Autoloading is part of the 'composer' that manages PHP packages and is a necessary feature of PHP.<\/p>\n\n\n\n<p>I will explain how to use it in a detailed and easy to understand manner.<\/p>\n\n\n\n<!--more-->\n\n\n\n<div class=\"wp-block-origin-block-msg-box\"><div class=\"msgbox msgbox-notice v1\"><i class=\"fas fa-exclamation-triangle icon\"><\/i><div class=\"msg\">\n<p>This is written by a Japanese who can't speak English with the help of translation application. Sorry if it's not good.<\/p>\n<\/div><\/div><\/div>\n\n\n<div id=\"toc\" class=\"idx-lst\">\n\t<input type=\"checkbox\" class=\"chk\" id=\"idx-lst-chk\" name=\"btn\" \/>\n\t<label class=\"idx-lst-hd\" for=\"idx-lst-chk\"><i class=\"fa-solid fa-check-double\"><\/i>Contents<\/label>\n\t<div class=\"idx-lst-bd\">\n\t\t<ul class=\"idx-lst-ul\">\n\t\t\t\t\t\t\t<li class=\"idx-lst-li idt0\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-1\" title=\"What is autoloading?\">What is autoloading?<\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"idx-lst-li idt0\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-2\" title=\"Let&#039;s use autoload!\">Let&#039;s use autoload!<\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"idx-lst-li idt1\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-3\" title=\"Creating composer.json file\">Creating composer.json file<\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"idx-lst-li idt1\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-4\" title=\"Installation of the autoload package\">Installation of the autoload package<\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"idx-lst-li idt1\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-5\" title=\"PHP sample program creation\">PHP sample program creation<\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"idx-lst-li idt1\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-6\" title=\"Editing composer.json\">Editing composer.json<\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"idx-lst-li idt1\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-7\" title=\"Updating the autoload map file\">Updating the autoload map file<\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"idx-lst-li idt1\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-8\" title=\"Let&#039;s run the program!\">Let&#039;s run the program!<\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"idx-lst-li idt0\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-9\" title=\"Updating the autoload map\">Updating the autoload map<\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"idx-lst-li idt1\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-10\" title=\"Be careful when using &#039;dump-autoload -o&#039; \">Be careful when using &#039;dump-autoload -o&#039; <\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"idx-lst-li idt0\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-11\" title=\"Non-class can also autoload\">Non-class can also autoload<\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"idx-lst-li idt0\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-12\" title=\"Laravel is already in\">Laravel is already in<\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"idx-lst-li idt0\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-13\" title=\"Summary of autoload\">Summary of autoload<\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"idx-lst-li idt0\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-14\" title=\"The types specified in autoload\">The types specified in autoload<\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t\t\t<li class=\"idx-lst-li idt0\">\n\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa-regular fa-hand-point-right ico\"><\/i>\n\t\t\t\t\t\t\t\t\t\t<a href=\"#index-list-15\" title=\"Development environment autoload\">Development environment autoload<\/a>\n\t\t\t\t<\/li>\n\t\t\t\t\t\t<\/ul>\n\t<\/div>\n<\/div>\n\n\n\n\n<h2 class=\"is-style-h2df\" id=\"index-list-1\">What is autoloading?<\/h2>\n\n\n\n<p>Autoloading is the ability to use PHP classes without 'require' a class file, just defining a 'use' operator.<\/p>\n\n\n\n<div class=\"pre-code-title\">How to use the class without autoload<\/div><pre class=\"\"><code class=\"language-php\">&lt;?php\nrequire \"TestClass1.php\";\nrequire \"TestClass2.php\";\nrequire \"TestClass3.php\";\n\nuse TestClass1;\nuse TestClass2;\nuse TestClass3;\n\n$test1 = new TestClass1();\n$test2 = new TestClass2();\n$test3 = new TestClass3();<\/code><\/pre>\n\n\n\n<div class=\"pre-code-title\">How to use autoload classes<\/div><pre class=\"\"><code class=\"language-php\">&lt;?php\nrequire vendor\/autoload.php\n\nuse TestClass1;\nuse TestClass2;\nuse TestClass3;\n\n$test1 = new TestClass1();\n$test2 = new TestClass2();\n$test3 = new TestClass3();<\/code><\/pre>\n\n\n\n<p>I don't get an error when I don't have TestClass* written anywhere and I haven't imported TestClass*.php.<\/p>\n\n\n\n<p>That's because TestClass* was already required by the autoload.<\/p>\n\n\n\n<p>In object-oriented programming, we use a lot of classes. And there is a rule of 'one file, one class'.<\/p>\n\n\n\n<p>If we don't have autoload, we have to write a lot of class file requirements. We have autoloading to avoid doing that.<\/p>\n\n\n\n<h2 class=\"is-style-h2df\" id=\"index-list-2\">Let's use autoload!<\/h2>\n\n\n\n<p>Let's use autoload before I explain. Just work in the same way.<\/p>\n\n\n\n<p>First, install the composer in the PHP environment, because you can't use it without the composer.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-a-link-ext alk-ex\"><a href=\"https:\/\/tadtadya.com\/en\/php-how-to-install-composer\/\" title=\"PHP, How to install composer\"><strong>PHP, How to install composer<\/strong> <i class=\"fas fa-angle-double-right\"><\/i><\/a><\/div>\n\n\n\n<h3 class=\"is-style-h3df\" id=\"index-list-3\">Creating composer.json file<\/h3>\n\n\n\n<p>Go to the PHP project home. Here is 'user-test\/php-test-autoload'.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<pre class=\"wp-block-code\"><code>...\u2500 php-test-autoload\n       \u251c\u2500 (empty)<\/code><\/pre>\n<\/div>\n\n\n\n<p>Still, the contents of the project are empty. Now run the command composers.<\/p>\n\n\n\n<div class=\"pre-code-title\">composer.json creation command<\/div><pre class=\"\" data-user=\"root\" data-output=\"\"><code class=\"language-bash\">composer init<\/code><\/pre>\n\n\n\n<p>You will be asked for various inputs, but for now, just press the Enter key.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<pre class=\"wp-block-code\"><code>...\u2500 php-test-autoload\n       \u251c\u2500 composer.json<\/code><\/pre>\n<\/div>\n\n\n\n<p>There should be a composer.json in the home of the project.<\/p>\n\n\n\n<div class=\"pre-code-title\">composer.json<\/div><pre class=\"line-numbers\"><code class=\"language-json\">{\n    \"name\": \"user-test\/php-test-autoload\",\n    \"authors\": [\n        {\n            \"name\": \"user-test\",\n            \"email\": \"user-test@gmail.com\"\n        }\n    ],\n    \"require\": {}\n}<\/code><\/pre>\n\n\n\n<p>The 'authors' can be empty. It must be set in php.ini and will not be added.<\/p>\n\n\n\n<h3 class=\"is-style-h3df\" id=\"index-list-4\">Installation of the autoload package<\/h3>\n\n\n\n<p>The project only has a composer.json. Next, install the autoload package.<\/p>\n\n\n\n<div class=\"pre-code-title\">Autoload installation command<\/div><pre class=\"\" data-user=\"root\" data-output=\"\"><code class=\"language-bash\">composer install<\/code><\/pre>\n\n\n\n<p>The autoload package has been installed.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default block\">\n<pre class=\"wp-block-code\"><code>...\u2500 php-test-autoload\n       \u251c\u2500 vendor\n       \u2502    \u251c\u2500 composer\n       \u2502    \u2502    \u251c\u2500 autoload_classmap.php\n       \u2502    \u2502    \u251c\u2500 autoload_namespaces.php\n       \u2502    \u2502    \u251c\u2500 autoload_psr4.php\n       \u2502    \u2502    \u251c\u2500 autoload_real.php\n       \u2502    \u2502    \u251c\u2500 autoload_static.php\n       \u2502    \u2502    \u251c\u2500 ClassLoader.php\n       \u2502    \u2502    \u251c\u2500 installed.php\n       \u2502    \u2502    \u2514\u2500 LICENSE\n       \u2502    \u2514\u2500 autoload.php\n       \u2514\u2500 composer.json<\/code><\/pre>\n<\/div>\n\n\n\n<p>The vendor directory is the installation location for the packages to be added to the 'require' of composer.json.<\/p>\n\n\n\n<p>Since autoload is a feature of the composer, it was installed in the 'vendor\/composer' directory.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<p>vendor\/autoload.php<\/p>\n<\/div>\n\n\n\n<p>There is also a program that runs autoload? Just load it with the 'require' operator and you can autoload it.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-note\">\n<p>Since autoload is a basic feature of the composer, it is always installed with the first 'composer install' command.<\/p>\n\n\n\n<p>Same thing if composer.json's 'require' is empty.<\/p>\n\n\n\n<p>The 'require' in compsoer.json is a list of packages to be installed in the project.<\/p>\n<\/div>\n\n\n\n<h3 class=\"is-style-h3df\" id=\"index-list-5\">PHP sample program creation<\/h3>\n\n\n\n<p>The autoload package has been added to the project. Next, add the sample program to the project.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default block\">\n<pre class=\"wp-block-code\"><code>...\u2500 php-test-autoload\n       \u251c\u2500 src\n       \u2502    \u251c\u2500 main.php\n       \u2502    \u251c\u2500 TestClass1.php\n       \u2502    \u251c\u2500 Test2\n       \u2502    \u2502    \u2514\u2500 TestClass2.php\n       \u2502    \u2514\u2500 Test3\n       \u2502         \u2514\u2500 TestClass3.php\n       \u251c\u2500 vendor\n       \u2502    \u251c\u2500 composer\n       \u2502    \u2502    \u251c\u2500 autoload_classmap.php\n       \u2502    \u2502    \u251c\u2500 autoload_namespaces.php\n       \u2502    \u2502    \u251c\u2500 autoload_psr4.php\n       \u2502    \u2502    \u251c\u2500 autoload_real.php\n       \u2502    \u2502    \u251c\u2500 autoload_static.php\n       \u2502    \u2502    \u251c\u2500 ClassLoader.php\n       \u2502    \u2502    \u251c\u2500 installed.php\n       \u2502    \u2502    \u2514\u2500 LICENSE\n       \u2502    \u2514\u2500 autoload.php\n       \u2514\u2500 composer.json<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"pre-code-title\">TestClass1.php<\/div><pre class=\"line-numbers\"><code class=\"language-php\">&lt;?php\n\nnamespace Origin;\n\nclass TestClass1 {\n    function __construct() {\n        echo \"new instance TestClass1\\n\";\n    }\n}<\/code><\/pre>\n\n\n\n<div class=\"pre-code-title\">TestClass2.php<\/div><pre class=\"line-numbers\"><code class=\"language-php\">&lt;?php\n\nnamespace Origin\\Test2;\n\nclass TestClass2 {\n    function __construct() {\n        echo \"new instance TestClass2\\n\";\n    }\n}<\/code><\/pre>\n\n\n\n<p>Do the same for TestClass3; note the difference in namespace between TestClass1, TestClass2 and TestClass3.<\/p>\n\n\n\n<p>It will come out later, but this time we will use psr4 coding conventions. psr4 has the following rules<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<ul><li>Class files must have namespace.<\/li><li>The namespace should match the directory configuration and name.<\/li><li>Namespace names start with a capital letter.<\/li><\/ul>\n<\/div>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-word\"><details><summary>PSR (PHP Standards Recommendations)\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<\/summary>\n<p><a href=\"https:\/\/www.php-fig.org\/psr\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/www.php-fig.org\/psr\/\">https:\/\/www.php-fig.org\/psr\/<\/a><\/p>\n\n\n\n<p>A standardization effort for PHP coding, developed by PHP-FIG.<\/p>\n\n\n\n<p><strong><span class=\"marker-green\">PHP-FIG (PHP Framework Interop Group)<\/span><\/strong><\/p>\n\n\n\n<p><a href=\"http:\/\/www.php-fig.org\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"php-fig.org\">http:\/\/www.php-fig.org\/<\/a><\/p>\n\n\n\n<p>PHP Framework Interoperability Group, an organization where PHP projects get together to discuss and coordinate the compatibility of each other's products.<\/p>\n\n\n\n<p>Many famous projects are participating.<\/p>\n<\/details><\/div>\n\n\n\n<div class=\"pre-code-title\">main.php<\/div><pre class=\"line-numbers\"><code class=\"language-php\">&lt;?php\n\nrequire \"vendor\/autoload.php\";\n\n\nuse Origin\\TestClass1;\nuse Origin\\Test2\\TestClass2;\nuse Origin\\Test3\\TestClass3;\n\nnew TestClass1();\nnew TestClass2();\nnew TestClass3();<\/code><\/pre>\n\n\n\n<h3 class=\"is-style-h3df\" id=\"index-list-6\">Editing composer.json<\/h3>\n\n\n\n<p>Register the directory where the class files are located, so that autoload can get them.<\/p>\n\n\n\n<p>Registration is done with the composer's configuration file, composer.json.<\/p>\n\n\n\n<div class=\"pre-code-title\">Register autoload to composer.json<\/div><pre class=\"line-numbers\"><code class=\"language-json\">{\n    \"name\": \"user-test\/php-test-autoload\",\n    \"authors\": [\n        {\n            \"name\": \"user-test\",\n            \"email\": \"user-test@gmail.com\"\n        }\n    ],\n    \"require\": {},\n    \"autoload\": {\n        \"psr-4\": {\n            \"Origin\\\\\": \"src\/\"\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>In autoload{}, the class files in the src directory are registered to begin with 'Origin'.<\/p>\n\n\n\n<p>In psr4, namespace names must start with a capital letter, so 'src' cannot be used.<\/p>\n\n\n\n<p>Here, when loading the class, we convert 'src' to 'Origin' to match the namespace.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<p>Match directory configuration and namespace configuration<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-note\">\n<p>The composer.json will be updated when you install the package using composer.<\/p>\n\n\n\n<p>The class of the package is automatically added to the autoload{}. You do not have to work by yourself.<\/p>\n\n\n\n<p>This time, I edited it directly in my own program and not in the composer package.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-note\">\n<p>If you edit composer.json directly, you must execute the following command.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<p>composer validate<\/p>\n<\/div>\n\n\n\n<p>This is the composer.json syntax check command. If you get an error, your edits are wrong.<\/p>\n\n\n\n<p>Be careful, as you will not be able to use composer commands.<\/p>\n<\/div>\n\n\n\n<h3 class=\"is-style-h3df\" id=\"index-list-7\">Updating the autoload map file<\/h3>\n\n\n\n<p>Update autoload.<\/p>\n\n\n\n<div class=\"pre-code-title\">autoload update command<\/div><pre class=\"\" data-user=\"root\" data-output=\"\"><code class=\"language-bash\">composer update<\/code><\/pre>\n\n\n\n<p>This updates the settings in composer.json to be precise. Since we added autoload{}, this will also update autoload.<\/p>\n\n\n\n<p>The mechanism of autoload is simple.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<p>composer install<\/p>\n\n\n\n<p>composer update<\/p>\n\n\n\n<p>composer require<\/p>\n<\/div>\n\n\n\n<p>Execute this command to create and update the map file.<\/p>\n\n\n\n<p>The contents of the map file is just an array, in which the classes and namespaces are defined.<\/p>\n\n\n\n<p>Autoload doesn't load a class, it just refers to the defined class.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default block\">\n<pre class=\"wp-block-code\"><code>...\u2500 php-test-autoload\n       \u2514\u2500 vendor\n            \u2514\u2500 composer\n                 \u251c\u2500 autoload_classmap.php\n                 \u251c\u2500 autoload_namespaces.php\n                 \u251c\u2500 autoload_psr4.php\n                 \u251c\u2500 autoload_static.php\n                 \u2514\u2500 installed.php<\/code><\/pre>\n<\/div>\n\n\n\n<p>You saw earlier, these files of autoload are map files.<\/p>\n\n\n\n<p>The autoload registration of psr4 in composer.json will be updated like this<\/p>\n\n\n\n<div class=\"pre-code-title\">autoload_psr4.php<\/div><pre class=\"line-numbers\"><code class=\"language-php\">&lt;?php\n\n\/\/ autoload_psr4.php @generated by Composer\n\n$vendorDir = dirname(dirname(__FILE__));\n$baseDir = dirname($vendorDir);\n\nreturn array(\n    'Origin\\\\' => array($baseDir . '\/src'),\n);<\/code><\/pre>\n\n\n\n<p>The conversion 'src' -&gt; 'Origin' is registered in the map.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-point\">\n<p>When the composer command updates the composer.json file, it expands the contents defined in autoload{} to the autoload map file.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-point\">\n<p>vendor\/autoload.php is a summary of all the map files. So only 'require'.<\/p>\n<\/div>\n\n\n\n<h3 class=\"is-style-h3df\" id=\"index-list-8\">Let's run the program!<\/h3>\n\n\n\n<p>The autoload sample program is now complete. Let's run it.<\/p>\n\n\n\n<div class=\"pre-code-title\">Execution Confirmation<\/div><pre class=\" command-line\" data-user=\"\" data-output=\"2-4\"><code class=\"language-bash\">php src\/main.php\nnew instance TestClass1\nnew instance TestClass2\nnew instance TestClass3<\/code><\/pre>\n\n\n\n<p>If you run it in the home of the project and get the same result, the usage of autoload is OK.<\/p>\n\n\n\n<h2 class=\"is-style-h2df\" id=\"index-list-9\">Updating the autoload map<\/h2>\n\n\n\n<p>Earlier, we updated the map file with this command.<\/p>\n\n\n\n<div class=\"pre-code-title\"><\/div><pre class=\"\" data-user=\"root\" data-output=\"\"><code class=\"language-bash\">composer update<\/code><\/pre>\n\n\n\n<p>But this command is for updating the composer.json file, not for updating the map.<\/p>\n\n\n\n<p>For example, when you add the source code, the class is not registered in the map.<\/p>\n\n\n\n<p>The composer.json has not been edited, so the \"composer update\" command will not work either.<\/p>\n\n\n\n<p>In that case, use the following command.<\/p>\n\n\n\n<div class=\"pre-code-title\">The autoload map update command<\/div><pre class=\"\" data-user=\"root\" data-output=\"\"><code class=\"language-bash\">composer dump-autoload<\/code><\/pre>\n\n\n\n<p>This reconstructs the autoload map <span class=\"marker-pink\">only<\/span>.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<p>Class not Found<\/p>\n<\/div>\n\n\n\n<p>Let's do this once it occurs.<\/p>\n\n\n\n<p>Surprisingly, sometimes it is not a bug in the program, but just the map is not updated.<\/p>\n\n\n\n<h3 class=\"is-style-h3df\" id=\"index-list-10\">Be careful when using 'dump-autoload -o'<\/h3>\n\n\n\n<p>Autoload has a map optimization feature called 'Autoloader Optimization' for fast loading.<\/p>\n\n\n\n<div class=\"pre-code-title\">Speeding up autoload<\/div><pre class=\"\" data-user=\"root\" data-output=\"\"><code class=\"language-bash\">composer dump-autoload -o<\/code><\/pre>\n\n\n\n<p>'-o' option. But be careful.<\/p>\n\n\n\n<p>With this option, the rules of psr-4 will be ignored when rebuilding the class map.<\/p>\n\n\n\n<p>Class files with namespace and directory configurations that do not conform to psr-4 will also be registered in the class map.<\/p>\n\n\n\n<p>You may suddenly get a lot of \"Class not Found\".<\/p>\n\n\n\n<p>This happens when you are updating a map with options and one time you update it without options. Surprisingly, this pattern is very common.<\/p>\n\n\n\n<p>It should be noted that this is a common occurrence in Laravel.<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table><tbody><tr><td>Development Environment<\/td><td>No options<\/td><\/tr><tr><td>Production Environment<\/td><td>Optional<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>It is better to use this usage.<\/p>\n\n\n\n<h2 class=\"is-style-h2df\" id=\"index-list-11\">Non-class can also autoload<\/h2>\n\n\n\n<p>Autoload can autoloading even outside of classes.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<ul><li>interface<\/li><li>function<\/li><li>const<\/li><\/ul>\n<\/div>\n\n\n\n<div class=\"pre-code-title\">A variety of autoload<\/div><pre class=\"\"><code class=\"language-php\">&lt;?php\nrequire vendor\/autoload.php;\n\nuse My\\Full\\ClassName as ClassName;\nuse My\\Full\\InterfaceName as IFName;\n\nclass Test extends ClassName implements IFName {\n    \/\/ ...\n}\n\u00a0\n\/\/ >= PHP 5.6\nuse function My\\Full\\functionName as func;\n\n$ret = func();\n\u00a0\n\/\/ >= PHP 5.6\nuse const My\\Full\\CONSTANT CONST_A;\n\n$val = CONST_A;<\/code><\/pre>\n\n\n\n<p>However, function and const are only supported by PHP version 5.6 or later.<\/p>\n\n\n\n<h2 class=\"is-style-h2df\" id=\"index-list-12\">Laravel is already in<\/h2>\n\n\n\n<p>The PHP web framework Laravel includes a composer.<\/p>\n\n\n\n<p>There is also composer.json and the autoload package in vendor\/composer.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<p>require vendor\/autoload.php<\/p>\n<\/div>\n\n\n\n<p>This is also not necessary. It is already implemented in public\/index.php.<\/p>\n\n\n\n<h2 class=\"is-style-h2df\" id=\"index-list-13\">Summary of autoload<\/h2>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-point\">\n<ul><li>Write \"require vendor\/autoload.php\" only once in the source code.<\/li><li>The use operator allows classes, interfaces, functions and const to be autoloaded.<\/li><li>Write the configuration in the composer.json file.<\/li><\/ul>\n<\/div>\n\n\n\n<p>This is the only basic usage of autoload.<\/p>\n\n\n\n<p>I'd like to say \"this is the end\", but I'll continue with the advanced part.<\/p>\n\n\n\n<p>Didn't you also describe the psr4 used in composer.json's autoload{} ?<\/p>\n\n\n\n<p>Other methods of specification are also described.<\/p>\n\n\n\n<div class=\"wp-block-wp-quads-adds\">\n<!-- WP QUADS v. 2.0.65.1  Shortcode Ad -->\n<div class=\"quads-location quads-ad\" id=\"quads-ad\" style=\"float:none;margin:0px;\">\n<ul class=\"quads-location-ul\">\r\n<li class=\"quads-location-li li-1\">\r\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block; text-align:center;\"\r\n     data-ad-format=\"fluid\"\r\n     data-ad-layout=\"in-article\"\r\n     data-ad-client=\"ca-pub-4926008942376207\"\r\n     data-ad-slot=\"5356287176\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script>\r\n<\/li>\r\n<li class=\"quads-location-li li-2\">\r\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block; text-align:center;\"\r\n     data-ad-format=\"fluid\"\r\n     data-ad-layout=\"in-article\"\r\n     data-ad-client=\"ca-pub-4926008942376207\"\r\n     data-ad-slot=\"5356287176\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script>\r\n<\/li>\r\n<\/ul><\/div>\n<\/div>\n\n\n\n<h2 class=\"is-style-h2df\" id=\"index-list-14\">The types specified in autoload<\/h2>\n\n\n\n<p>The composer.json's autoload{} can be specified in a variety of ways other than psr-4.<\/p>\n\n\n\n<p>Let's take a look at them one by one.<\/p>\n\n\n\n<h3 class=\"is-style-h3df\">files<\/h3>\n\n\n\n<div class=\"pre-code-title\"><\/div><pre class=\"\"><code class=\"language-json\">\"autoload\": {\n    \"files\": [\n        \"src\/functions\/func1.php\",\n        \"src\/const\/const1.php\",\n    ]\n}<\/code><\/pre>\n\n\n\n<p>Autoload functions and const from .php files. Use it outside of classes and interfaces.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<p><strong><span class=\"marker-green\">Map files<\/span><\/strong><\/p>\n\n\n\n<p>vendor\/composer\/autoload_files.php<\/p>\n<\/div>\n\n\n\n<h3 class=\"is-style-h3df\">psr-4<\/h3>\n\n\n\n<div class=\"pre-code-title\"><\/div><pre class=\"\"><code class=\"language-json\">\"autoload\": {\n    \"psr-4\": { \"Vendor\\\\\": \"src\/vendor\/\" }\n}<\/code><\/pre>\n\n\n\n<div class=\"pre-code-title\">Multiple specifications<\/div><pre class=\"\"><code class=\"language-json\">\"autoload\": {\n    \"psr-4\": { \"Vendor\\\\\": [\"src\/Vendor\/\", \"lib\/Vendor\/\"] }\n}<\/code><\/pre>\n\n\n\n<p>Use in Laravel and other psr-compliant ones.<\/p>\n\n\n\n<p>The psr-4 namespace and directory configuration should match.<\/p>\n\n\n\n<p>Specify the directory location of its top-level namespace directory.<\/p>\n\n\n\n<p>You can also specify more than one, enclosed in [].<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-note\">\n<p>In PHP it is normal to conform to the PSR, and it is rare not to participate like in WordPress.<\/p>\n\n\n\n<p>However, there are many projects that do not participate.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-note\">\n<p>When the namespace and directory configuration do not match in psr-4, an error occurs during application execution.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<p>Class not Found<\/p>\n<\/div>\n\n\n\n<p>The error of the package itself does not occur, so it looks as if the application has a bug.<\/p>\n\n\n\n<p>The map information in autoload may be outdated or a bug in the package.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-point\">\n<p>psr-4 also includes what you set up in psr-0.<\/p>\n\n\n\n<p> No need for psr-0 settings, which will be explained later.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<p><strong><span class=\"marker-green\">Map files<\/span><\/strong><\/p>\n\n\n\n<p>vendor\/composer\/autoload_psr-4.php<\/p>\n\n\n\n<p>vendor\/composer\/autoload_static.php<\/p>\n\n\n\n<p>vendor\/composer\/autoload_classmap.php<\/p>\n<\/div>\n\n\n\n<h3 class=\"is-style-h3df\">psr-0<\/h3>\n\n\n\n<div class=\"pre-code-title\"><\/div><pre class=\"\"><code class=\"language-json\">\"autoload\": {\n    \"psr-0\": {\n        \"Vendor\\\\\": \"src\/Vendor\/\",\n        \"Vendor\\\\Namespace\\\\\": \"src\/Vendor\/\",\n        \"Vendor_Namespace_\": \"src\/\"\n    }\n}<\/code><\/pre>\n\n\n\n<p>The current level of psr is predominantly 4.<\/p>\n\n\n\n<p>Since larger psr levels include lower levels, psr-0 is rarely used.<\/p>\n\n\n\n<p>Use it with older source code, such as projects that have been around for a few years, or those that are up to psr-2 compliant.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-note\">\n<p>There is no psr-2 in composer autoload.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<p><strong><span class=\"marker-green\">Map files<\/span><\/strong><\/p>\n\n\n\n<p>vendor\/composer\/autoload_classmap.php<\/p>\n<\/div>\n\n\n\n<h3 class=\"is-style-h3df\">classmap<\/h3>\n\n\n\n<div class=\"pre-code-title\"><\/div><pre class=\"\"><code class=\"language-json\">\"autoload\": {\n    \"classmap\": [\"src\/\", \"lib\/\", \"Something.php\"]\n}<\/code><\/pre>\n\n\n\n<p>Use when not complying with psr, such as WordPress.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<p><strong><span class=\"marker-green\">Map files<\/span><\/strong><\/p>\n\n\n\n<p>vendor\/composer\/autoload_classmap.php<\/p>\n<\/div>\n\n\n\n<h3 class=\"is-style-h3df\">exclude-from-classmap<\/h3>\n\n\n\n<div class=\"pre-code-title\"><\/div><pre class=\"\"><code class=\"language-json\">\"autoload\": {\n    \"exclude-from-classmap\": [\"\/Tests\/\", \"\/test\/\", \"\/tests\/\"]\n}<\/code><\/pre>\n\n\n\n<p>Set the autoload exclusion.<\/p>\n\n\n\n<p>Specify classes that are not used in the production environment, such as test classes.<\/p>\n\n\n\n<div class=\"wp-block-origin-block-em-box em-group em-group-default\">\n<p><strong><span class=\"marker-green\">Map files<\/span><\/strong><\/p>\n\n\n\n<p>vendor\/composer\/autoload_classmap.php<\/p>\n<\/div>\n\n\n\n<h2 class=\"is-style-h2df\" id=\"index-list-15\">Development environment autoload<\/h2>\n\n\n\n<p>Set the autoload of the development environment.<\/p>\n\n\n\n<p>Specify the test class and so on. The only directory that can be specified is the root directory.<\/p>\n\n<!-- WP QUADS Content Ad Plugin v. 2.0.65.1 -->\n<div class=\"quads-location quads-ad2\" id=\"quads-ad2\" style=\"float:none;margin:0px;\">\n<ul class=\"quads-location-ul\">\r\n<li class=\"quads-location-li li-1\">\r\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block\"\r\n     data-ad-client=\"ca-pub-4926008942376207\"\r\n     data-ad-slot=\"6550760310\"\r\n     data-ad-format=\"auto\"\r\n     data-full-width-responsive=\"true\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script>\r\n<\/li>\r\n<li class=\"quads-location-li li-2\">\r\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block\"\r\n     data-ad-client=\"ca-pub-4926008942376207\"\r\n     data-ad-slot=\"6550760310\"\r\n     data-ad-format=\"auto\"\r\n     data-full-width-responsive=\"true\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script>\r\n<\/li>\r\n<\/ul>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>PHP autoloading is the ability to define a 'use' operator to automatically load a class. It does not use the 'require' operator. Autoloading is part of the 'composer' that manages PHP packages and is a necessary feature of PHP. I will explain how to use it in a detailed and easy to understand manner.<\/p>\n","protected":false},"author":1,"featured_media":12485,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"php composer: Let's make effective use of autoload !\r\n\r\n#php\r\n#composer\r\n#php_composer_Let_s_make_effective_use_of_autoload","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[9,43,179],"tags":[265,369,381],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/tadtadya.com\/en\/wp-content\/uploads\/sites\/2\/2018\/10\/php-1.png?fit=1280%2C671&ssl=1","jetpack_sharing_enabled":false,"jetpack_shortlink":"https:\/\/wp.me\/p8Qw77-1re","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/posts\/5532"}],"collection":[{"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/comments?post=5532"}],"version-history":[{"count":0,"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/posts\/5532\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/media\/12485"}],"wp:attachment":[{"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/media?parent=5532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/categories?post=5532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tadtadya.com\/en\/wp-json\/wp\/v2\/tags?post=5532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}