← Other topics

Using Composer in a plain PHP project (No frameworks)

Video Notes

Many PHP developer’s only experience with Composer is via its integration with frameworks like Laravel or Symfony. In this guide, we’ll learn how to use Composer in a very simple PHP project that isn’t using a framework.

If Composer is not already installed on your system:

Setup

To start using Composer in a project, all you need is a composer.json file within that project.

Knowing that, create a new file composer.json in the root of your project with these starting configs:

{
    "name": "vendor-name/project-name",
    "require": {
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    }
}

To begin, the only thing you have to customize is the name attribute. This attribute should follow the pattern of vendor-name/project-name and must be all lowercase and contain only letters, numbers, or hyphens. You can learn more about naming here....

With the above config file in place, you can run composer update to set up Composer in the project.

Running composer update builds a composer.json file and vendor directory

Requiring packages

Like any Composer-based project, you can add new packages by adding them to the require section of your composer.json file, or via the command composer require vendor-name/package-name.

For an example, lets add the package ajaz/random-quotes-generator-php which can be used to generate random quotes.

> composer require ajaz/random-quotes-generator-php

Using external packages

Any file in which you want to use your Composer-imported packages must include the Composer autoload file (vendor/autoload.php) as well as use statements for the given classes you want to use. Here’s an example index.php file that uses the ajaz/random-quotes-generator-php package:

<?php

# file: /index.php

# Pull in Composer autoloading functionality
require 'vendor/autoload.php';

# Make the RandomQuotes class accessible within this file
use RandomQuotes\RandomQuotes;

# Use the RandomQuotes class
$rq = new RandomQuotes();
$quote = $rq->generate();
print_r($quote);

Using your own classes

Our starting composer.json file included some psr-4 autoloading configs that will allow us to map our own classes within this project:

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

This config tells Composer that when we utilize classes in the App namespace, it should look for those classes in the src/ directory. The name App and the directory of src/ is arbitrary - you can change these to whatever you want.

To demonstrate using our own classes with this autoloading config, create a class at src/Demo.php with these contents:

<?php

namespace App;

class Demo
{
    public static function test()
    {
        return 'Testing...';
    }
}

Then, adding on to our example index.php file, we can utilize this class:

<?php

# Pull in Composer autoloading functionality
require 'vendor/autoload.php';

use App\Demo; # ⭐ NEW - Make our own class accessible
use RandomQuotes\RandomQuotes;

print_r(Demo::test()); # ⭐ NEW - Use our own class

# Use the RandomQuotes class
$rq = new RandomQuotes();
$quote = $rq->generate();
print_r($quote);
If this info helped you out, you can send a tip via PayPal. Thanks for your support!
← Other topics