Useful Composer PHP Package Manager Commands

Useful Composer PHP 7 Package Manager Commands

==========================

 

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 file that details the exact
versions it has installed. When using version control it is important that you commit this file
(alongside the composer.json file), don’t add it to your .gitignore file if you’re on Git.
This is very important because the lock file details the exact version of a package that was
installed at a particular time in your version control system. You can, however, exclude a
directory called vendor,

So now we can add the following markup to the composer.json file:

{
  "autoload": {
    "psr-4": {
      "IcyApril\\ChapterOne": "src/"
    }
  }
}
So let me explain what this file does; it tells Composer to autoload everything in the

So, the next step is to create our src directory where we include the code we want to
autoload. Done that? Right, now let's open up our command line and move into the
directory where we've put our composer.json file.
In order to install everything in the composer.json file in your project just run the
composer install command. For subsequent updates, the composer update command
will update to the latest versions of all dependencies as defined in composer.json. If you
don't want to do this, though, there is an alternative; running the composer dump-autoload
command will solely regenerate the list of the PSR-0/PSR-4 classes that need to
be included in the project (for example, you add, delete, or rename some of your classes).
Now let me cover how you will actually go about creating a class. So, let's create an src
directory in our project and in that src directory create a new class called Book. You can do
this by creating a file called Book.php. In that file, add something like this:

<?php
namespace IcyApril\ChapterOne;

class Book
{
  public function __construct()
  {
    echo "Hello world!";
  }
}


?>

The line after you put in your PHP opening tag, we need to pull in our autoloader script:
require_once('vendor/autoload.php');

Then we can instantiate our Book class:
new \IcyApril\ChapterOne\Book();