{"id":4271,"date":"2017-10-26T12:09:23","date_gmt":"2017-10-26T10:09:23","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=4271"},"modified":"2020-02-09T10:59:41","modified_gmt":"2020-02-09T09:59:41","slug":"autoloader-composer","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/autoloader-composer-4271\/","title":{"rendered":"Create autoloader with Composer"},"content":{"rendered":"\n<p>I started a small WordPress plugin and tried to orientate myself a little bit towards the <a href=\"https:\/\/github.com\/schlessera\/as-speaking\/\">Speaking Plugin<\/a> of <a href=\"https:\/\/twitter.com\/schlessera\">Alain Schlesser<\/a> for the structure and basic principles. So I came about the topic autoloading. Alain wrote a custom autoloader for the plugin but mentioned the possibility via composer in his <a href=\"https:\/\/wordpress.tv\/2017\/10\/14\/alain-schlesser-oop-plugin-development-basics\/\">talk at the WordCamp Nijmegen<\/a>. This post describes how to create an autoloader with <a href=\"https:\/\/getcomposer.org\/\">Composer<\/a>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Of course, you need Composer installed. Now we run <code class=\"lang-bash\">composer init<\/code> in our project folder and enter a few information \u2013 as a result, we get a <code class=\"lang-markup\">composer.json<\/code>.<\/p>\n\n\n\n<p>After that, we need to add a small additional piece into the <code class=\"lang-markup\">composer.json<\/code>, so we can create the autoloader in the next step. This section looks like that in my plugin (helpful for the whole autoloading thing with Composer was an <a href=\"http:\/\/phpenthusiast.com\/blog\/how-to-autoload-with-composer\">article from phpenthusiast.com<\/a>):<\/p>\n\n\n<pre class=\"wp-block-code lang-json\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-string\">\"autoload\"<\/span>: {\n  <span class=\"hljs-string\">\"psr-4\"<\/span>: {\n    <span class=\"hljs-string\">\"FlorianBrinkmann\\\\CustomizeThemesInstaller\\\\\"<\/span>: <span class=\"hljs-string\">\"src\/\"<\/span>\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>We map the namespace <code class=\"lang-php\">FlorianBrinkmann\\CustomizeThemesInstaller<\/code> to the <code class=\"lang-markup\">src<\/code> directory, so that, for example, the class <code class=\"lang-php\">FlorianBrinkmann\\CustomizeThemesInstaller\\Plugin<\/code> is searched in the file <code class=\"lang-markup\">src\/Plugin.php<\/code> (PSR-4 requires the usage of namespaces).<\/p>\n\n\n\n<p>Now we run <code class=\"lang-bash\">composer dump-autoload<\/code> to create the autoloader and include it into our bootstrap file via <code class=\"lang-php\">require_once __DIR__ . '\/vendor\/autoload.php';<\/code>.<\/p>\n\n\n\n<p>For the production, it is recommended to optimize the autoloader, so it does not need to scan the file system before resolving a class name. There are different ways to optimize it, as described in the <a href=\"https:\/\/getcomposer.org\/doc\/articles\/autoloader-optimization.md\">Composer docs<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I started a small WordPress plugin and tried to orientate myself a little bit towards the Speaking Plugin of Alain Schlesser for the structure and basic principles. So I came about the topic autoloading. Alain wrote a custom autoloader for the plugin but mentioned the possibility via composer in his talk at the WordCamp Nijmegen. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-4271","post","type-post","status-publish","format-standard","hentry","category-web-development"],"wp-worthy-pixel":{"ignored":false,"public":null,"server":null,"url":null},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=4271"}],"version-history":[{"count":11,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4271\/revisions"}],"predecessor-version":[{"id":5838,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4271\/revisions\/5838"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=4271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=4271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=4271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}