#php

Setup a small PHP project to run with namespace and autoload by Composer

Let’s setup a project with autoload and namespace by composer.

1. Install composer on your machine.

2. cd terminal to your project directory then run composer init

Terminal screen shows:

Welcome to the Composer config generator

This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [username/src]:
Description []:
Author [anttr <useremail@mailhost.com>, n to skip]:
Minimum Stability []:
Package Type (e.g. library, project, metapackage, composer-plugin) []:
License []:

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]?
Search for a package:
Would you like to define your dev dependencies (require-dev) interactively [yes]?
Search for a package:

{
    "name": "username/src",
    "authors": [
        {
            "name": "username",
            "email": "username@mailhost.com"
        }
    ],
    "require": {}
}

Do you confirm generation [yes]?

Step by step type your information follow instructions then hit enter. The new file composer.json will be generated in your project directory.

3. Next, run command composer install. After composer install successfully it will have the new folder name vendor in project directory. Take a look to project directory again:

snippets (my project directory)
|---vendor
|   |---composer
|   |   |   autoload_classmap.php
|   |   |   autoload_namespaces.php
|   |   |   autoload_psr4.php
|   |   |   autoload_real.php
|   |   |   autoload_static.php
|   |   |   ClassLoader.php
|   |   |   installed.json
|   |   |   LICENSE
|   |   |
|   |   autoload.php
|   |
|   composer.json

4. Define autoload mapping directory in composer.json

{
  "name": "anttr/snippets",
  "authors": [
    {
      "name": "AN Tran",
      "email": ""
    }
  ],
  "require": {},
  "autoload": {
    "psr-4": {      "App\\": ""    }  }
}

The line "App\\": "" show that the autoload will treat my project directory "" as namespace App\. And it will follow PSR-4 standard.

5. Update composer by running composer update. Wait till it complete updating.

6. Everything now done, let’s create some code to test.

I create a folder name Post20180422, then put some code inside:

snippets
|---Post20180422
|   |---AtHome
|   |   |   Me.php
|   |   |
|   |---AtWork
|   |   |   Me.php
|   |   |
|   |   index.php
|   |
|---vendor
|   |
|   composer.json

File contents:

<?php
// Post20180422/AtHome/Me.php

namespace App\Post20180422\AtHome;
class Me
{
  public function partyAllNight()
  {
    echo "Party All Night<br>";
  }
}

<?php
// Post20180422/AtWork/Me.php

namespace App\Post20180422\AtWork;
class Me
{
  public function sleepAllDay()
  {
    echo "Sleep All Day<br>";
  }
}

// Post20180422/index.php
<?php

/* To use autoload, in the first line of PHP project script, it must to require this line */
require_once __DIR__ . "/../vendor/autoload.php";
/* And now it is able to call a class with its namespace */
$meAtHome = new App\Post20180422\AtHome\Me();$meAtHome->partyAllNight();

$meAtWork = new App\Post20180422\AtWork\Me();$meAtWork->sleepAllDay();

All of explanations I’ve just put them to the code. Now, it’s time to try it yourself :).

An Tran

An Tran

A passionate web developer, self-learner and music lover.

Read More