How To Build A PHP Package

Wei Zhang
4 min readFeb 17, 2019

The first time I learn to build a PHP package is through a job interview. The technical part required me to build a PHP package with PSR-4 standard. In short, PSR-4 is a PHP standard to autoload PHP classes and files. For more information https://www.php-fig.org/psr/psr-4/.

There are some benefits to build system in packages. Based on my observation and experience, a major advantage it brings is break down the complexity and responsibility from application, which also make the system design loosely coupled.

Package is allowed to be shared across multiple applications. Thus, it also benefits to large scale system’s design like microservices architecture and common used by component based system.

A personal reason to learn this is to make better contribution in open source project, since all the open source project was implementing the standard. It has no doubt that contributing open source can actually learn lots more and sharpen our skill.

Skeleton Design

Skeleton design of package is always my first step on package development. Although this is completely a developer’s preference, Spatie’s packages is a good reference. They made a good standard for their group, this skeleton package is a pretty good reference.

Package skeleton

Composer Configuration

Second step, runcomposer init in terminal, then provide the information like package name, description, type, license, authors and etc…

composer init

After composer.json file been generated, add the key autoload and autoload-dev with psr-4 as sub-key in the composer.json to autoload the classes and test scripts.

Composer autoload

Test Configuration

Test script is something that “must have” in package development. Without test scripts, you won’t know if the code is executable. Good testing environment can make it convenient to run. I will use phpunit to run test, run following command to install dependencies.

composer require phpunit/phpunit — dev

After this, you can see the phpunit package was installed and added inside require-dev with the package version.

Install phpunit

After all, add a file with file name phpunit.xml in root directory of package. The file will be loaded by phpunit to run tests. Test script directory and options will be defined inside the file phpunit.xml.

my-package phpunit.xml

Test script can be run with command:

vendor/bin/phpunit

Meanwhile, you can configure script in composer.json. This is the best when you like to run command with custom options. For example, I want to run it with colours.

configure command for composer test

This configuration will allow to run all tests by running `composer test`.

Environment is ready, now is time to write some code…

Let write a test case for a simple addition function.

AdditionTest

Then, write the functional class.

Addition

Package is complete!!! Now commit the source code to Github.

Create a readme file and make the first commit.

Readme file and first commit

Then, configure .gitignore file and commit the source code. This sample has committed here:

https://github.com/LeeWeiZhang/my-package

Now it is ready to used by other applications.

A sample Laravel application will used as sample to run function from the package. Naturally any application with composer will be able to install this package as well.

Configure “repositories” field in composer.json in Laravel application.

composer.json repositories

Then run command to install package

composer require vendor/package

In this example, I run

composer require weizhang/my-package

If the package is committed inside a private git repository, you will be asked for token from your Git account.

Then call the function from the application. Because I was lazy to create a controller 😜, so I do it in routes/web.php for testing.

Use package from Laravel

Run it from web browser.

run in browser

1+2+3+4=10. Yess It works!!! 😎😎😎

LEARNING NEVER END

There are much more to learn in package development, and I think this is something good to start with.

By the way, there are few things in my mind that need to learn in package development; maybe it will be in next article. If you have something on mind, please do not hesitate to comment.

--

--

Wei Zhang

Software Engineer, based in Malaysia. 90% backend 10% frontend. PHP, Typescript, JavaScript, NodeJs, MySQL, DynamoDB, PostgreSQL, VueJS, ReactJS, Laravel