About composer.json generator
Create a composer.json for a PHP package with autoloading, requirements and metadata.
Autoloading
PSR-4 autoloading maps a namespace to a folder, so classes load automatically without manual require statements.
Fields
- name in vendor/package form.
- require lists dependencies and the PHP version.
- autoload maps namespaces to paths.
A worked example
A package named acme/tools requires PHP ^8.1 and monolog/monolog ^3.0, with PSR-4 autoloading that maps the namespace Acme\Tools\ to src/. After composer install, class Acme\Tools\Mailer in src/Mailer.php loads automatically through vendor/autoload.php.
Tips
- Commit composer.lock for applications, but not necessarily for libraries.
- Use require-dev for test tools.
- Choose a licence and a clear vendor/package name.
Publishing
To share the package, push it to a public repository and register it on Packagist, so that others can install it with composer require vendor/package.
Scripts
The scripts section can define commands such as composer test, which runs your test suite with one short instruction.
