{"id":981,"date":"2019-06-30T15:52:17","date_gmt":"2019-06-30T12:52:17","guid":{"rendered":"http:\/\/leonidassavvides.com\/blog\/?p=981"},"modified":"2019-06-30T15:52:17","modified_gmt":"2019-06-30T12:52:17","slug":"useful-composer-php-package-manager-commands","status":"publish","type":"post","link":"https:\/\/www.leonidassavvides.com\/blog\/2019\/06\/useful-composer-php-package-manager-commands\/","title":{"rendered":"Useful Composer PHP Package Manager Commands"},"content":{"rendered":"<div style=\"margin-top: 0px; margin-bottom: 0px;\" class=\"sharethis-inline-share-buttons\" ><\/div><p style=\"text-align: center;\"><strong>Useful Composer PHP 7 Package Manager <span style=\"color: #008000;\">Commands<\/span><\/strong><\/p>\n<p style=\"text-align: center;\"><strong>==========================<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Composer is fairly easy to update, just run this command:<br \/>\n<span style=\"color: #008000;\"><strong>composer self-update<\/strong><\/span><\/p>\n<p>Composer works by using the configuration in a file called <strong>composer.json<\/strong>, where you can<br \/>\noutline external dependencies and your autoloading style. Once Composer has installed<br \/>\ndependencies listed in this file, it writes a <strong>composer.lock<\/strong> file that details the exact<br \/>\nversions it has installed. When using version control it is important that you commit this file<br \/>\n(alongside the <strong>composer.json<\/strong> file), don&#8217;t add it to your <strong>.gitignore<\/strong> file if you&#8217;re on Git.<br \/>\nThis is very important because the lock file details the exact version of a package that was<br \/>\ninstalled at a particular time in your version control system. You can, however, exclude a<br \/>\ndirectory called <strong>vendor<\/strong>,<\/p>\n<p>So now we can add the following markup to the composer.json file:<\/p>\n<pre>{\n  \"autoload\": {\n    \"psr-4\": {\n      \"IcyApril\\\\ChapterOne\": \"src\/\"\n    }\n  }\n}\nSo let me explain what this file does; it tells Composer to autoload everything in the\n\nSo, the next step is to create our <strong>src<\/strong> directory where we include the code we want to\nautoload. Done that? Right, now let's open up our command line and move into the\ndirectory where we've put our <strong>composer.json<\/strong> file.\nIn order to install everything in the <strong>composer.json<\/strong> file in your project just run the\n<span style=\"color: #008000;\"><strong>composer install<\/strong><\/span> command. For subsequent updates, the <span style=\"color: #008000;\"><strong>composer update<\/strong><\/span> command\nwill update to the latest versions of all dependencies as defined in <strong>composer.json<\/strong>. If you\ndon't want to do this, though, there is an alternative; running the <span style=\"color: #008000;\"><strong>composer dump-autoload<\/strong><\/span>\ncommand will solely regenerate the list of the PSR-0\/PSR-4 classes that need to\nbe included in the project (for example, you add, delete, or rename some of your classes).\nNow let me cover how you will actually go about creating a class. So, let's create an src\ndirectory in our project and in that src directory create a new class called Book. You can do\nthis by creating a file called Book.php. In that file, add something like this:\n\n&lt;?php\nnamespace IcyApril\\ChapterOne;\n\nclass Book\n{\n  public function __construct()\n  {\n    echo \"Hello world!\";\n  }\n}\n\n\n?&gt;\n\nThe line after you put in your PHP opening tag, we need to pull in our autoloader script:\n<strong>require_once('vendor\/autoload.php');\n<\/strong>\nThen we can instantiate our Book class:\n<strong>new \\IcyApril\\ChapterOne\\Book();<\/strong>\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Useful Composer PHP 7 Package Manager Commands ========================== &nbsp; Composer is fairly easy to update, just run this command: composer self-update Composer works by using the configuration in a file called composer.json, where you can outline external dependencies and your autoloading style. Once Composer has installed dependencies listed in this file, it writes a composer.lock &hellip; <a href=\"https:\/\/www.leonidassavvides.com\/blog\/2019\/06\/useful-composer-php-package-manager-commands\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Useful Composer PHP Package Manager Commands&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-981","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/posts\/981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/comments?post=981"}],"version-history":[{"count":0,"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/posts\/981\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/media?parent=981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/categories?post=981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.leonidassavvides.com\/blog\/wp-json\/wp\/v2\/tags?post=981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}