Back to results for “Unit Testing with PHPUnit”
PHP › Package Development & Publishing with Composer

What key sections should a well structured composer.json file for a publishable package include, beyond the basic name and dependencies?

Intermediate

Answer

A well structured package composer.json typically includes a clear description explaining what the package does, an appropriate open source license declaration, author information, a properly configured autoload section following PSR-4 conventions, a require section listing genuine runtime dependencies, and a require-dev section listing development only dependencies like a testing framework, which are all important both for usability by other developers and often required for acceptance onto Packagist.
{
  "name": "mycompany/validation-helpers",
  "description": "Reusable validation rules for PHP applications",
  "license": "MIT",
  "require": {"php": ">=8.1"},
  "require-dev": {"phpunit/phpunit": "^10.0"}
}
Real-world example A developer preparing to publish their first open source package carefully fills out every recommended field in composer.json, including a clear license and description, making the package immediately usable and trustworthy to other developers discovering it.

Common follow-ups: Why is declaring an explicit license important for an open source package?;What is the difference between require and require-dev for a package specifically, as opposed to an application?