Programster's Blog

Tutorials focusing on Linux, programming, and open-source

PHP - Specifying Package Requirements

Requiring Other Packages

When writing packages, it's common for them to require other packages that they work with. For example, my AWS wrapper package simply makes it easier for me to work with the official AWS SDK, so it requires that package to work. This is simple enough to specify in the requirements with a block in the composer.json file like so:

"require": {
    "aws/aws-sdk-php" : "3.24.*"
},

PHP Version

You may also need to specify that your package will only work with a certain version of PHP. For example, this may be because your package is using return types, which were introduced in 7.0, so your package would not work with developers using PHP 5.6. Also, if you are using the void return type, this was only introduced in 7.1. This is easy enough to specify with:

"require": {
    "php": ">=7.0.0",
},

Required Extensions

Finally, your package may require that certain extensions are installed. For example, the AWS SDK actually requires the xml extension for PHP to be installed for the code to work. Unfortunately, they don't specify this, so you can find yourself in a position where your code just blocks forever and never returns. This can easily be remedied with the example below:

"require": {
    "ext-libxml" : "*",
    "ext-simplexml" : "*"
},

Figuring out the relevant require text to put in here is actually the hard part for me so I'm going to list the common ones I come across below:

  • ext-libxml
  • ext-simplexml
  • ext-mcrypt
  • ext-mysqli

If there is one that you think needs adding, please suggest it in the comments below.

Last updated: 16th September 2021
First published: 16th August 2018

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch