Sublimity Dev Blog

Articles and Tips in Web Development , Programming , SEO and Linux

Recent Posts

Published On

Modern PHP Project from Scratch with Composer: Setup, Structure & Commands

Composer is the de facto standard for PHP dependency management and project setup. Whether you're building a simple tool or a full-scale web application, using Composer properly ensures clean structure, reusability, and better team collaboration. In this guide, we'll walk through how to create a modern PHP project from scratch using Composer, with best practices in folder layout, namespacing, and command usage.

1. Installing Composer

Before you begin, make sure Composer is installed on your system. You can check this by running:

composer --version

If not installed, follow the official instructions from getcomposer.org.

2. Initialize a New PHP Project

Create a new project folder and initialize it with Composer:

mkdir my-php-app
cd my-php-app
composer init

Follow the interactive prompts to set project name, description, license, and dependencies.

3. Recommended Project Structure

Here’s a clean and scalable folder structure:

my-php-app/
├── src/
│   └── App/
│       └── Example.php
├── tests/
├── vendor/
├── composer.json
├── composer.lock
└── index.php

- src/ contains your application code
- tests/ for unit tests
- vendor/ is where Composer installs dependencies
- composer.json is your configuration file

4. Using Namespaces

Modern PHP uses PSR-4 autoloading with namespaces. Update composer.json to reflect your namespace:

{
  "autoload": {
    "psr-4": {
      "App\\": "src/App/"
    }
  }
}

After updating, run:

composer dump-autoload

Example of a namespaced class:

<?php

namespace App;

class Example {
  public function hello() {
    return "Hello from App\\Example!";
  }
}

5. Creating an Entry Point

Create index.php as your application entry point:

<?php

require __DIR__ . '/vendor/autoload.php';

use App\Example;

$example = new Example();
echo $example->hello();

6. Adding Dependencies

To add a package from Packagist:

composer require monolog/monolog

This adds Monolog and updates your composer.json.

7. Composer Scripts (Optional)

Define scripts in composer.json to automate tasks:

"scripts": {
  "start": "php -S localhost:8000"
}

Run with:

composer start

8. Testing and Autoloading

You can use PHPUnit for tests. Install it:

composer require --dev phpunit/phpunit

Example test structure:

tests/
└── ExampleTest.php
<?php

use PHPUnit\Framework\TestCase;
use App\Example;

class ExampleTest extends TestCase {
  public function testHello() {
    $e = new Example();
    $this->assertEquals("Hello from App\\Example!", $e->hello());
  }
}

9. Common Composer Commands

  • composer init – Initialize a new project
  • composer install – Install dependencies from composer.lock
  • composer update – Update all dependencies
  • composer dump-autoload – Rebuild autoload files
  • composer require package/name – Add new package
  • composer remove package/name – Remove a package
  • composer show – List installed packages

10. Final Notes

Composer enables modern PHP development by bringing structure, dependency management, and autoloading into every project. Following best practices from the beginning ensures your codebase remains clean and scalable. This guide should provide you with a practical and professional starting point for all your Composer-based PHP projects.