PHP Packages Introduction - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Packages Introduction - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript


Post Top Ad

Post Top Ad

Wednesday, July 17, 2019

PHP Packages Introduction

PHP Packages


Packages and libraries allow you to not reinvent the wheel. Instead, you’re able to use the wheels created by others in your projects. This chapter shows you how to use three collections of packages: Composer, PEAR, and PECL. Each provides a set of tools for you to easily incorporate packages into your own code, and even contribute packages of your own back to the common collective.

Composer is a dependency manager. Use it to install the code, including any external packages, needed by the libraries you want use. Composer makes it simple to wrangle the proper versions of code into your projects, both when you begin the project and when you decide to upgrade a package to a newer release. Packagist, the primary place to find Composer packages, has tens of thousands PHP packages.

PEAR is the PHP Extension and Application Repository, a collection of open source classes that work together. Developers can use PEAR classes to parse XML, implement authentication systems, generate CAPTCHAs, send MIME mail with attachments, and a wide variety of other common (and not so common) tasks. A pear is also a tasty fruit.

PECL is the PHP Extension Community Library. PECL, pronounced “pickle,” is a series of extensions to PHP written in C. These extensions are just like the ones distributed with the main PHP release, but they’re of more specialized interest—such as a MongoDB database driver or an OAuth 1.0 extension.

Each of these three collections fills a different need. Composer accepts all contributions. Anyone can register their package for use by anyone. There are often multiple packages that all solve similar problems, and quality can be anywhere from first class to highly buggy. In contrast, PEAR is a vetted set of code with one best implementation of any area, each working in conjunction with another. It’s like The Cathedral and the Bazaar. And where Composer and PEAR both handle code written in PHP, PECL covers the C extension side of PHP’s world.

PEAR and PECL also install one version of a library to be used across all projects. In theory, this simplifies management because there’s only one set of files to keep up-to-date. However, in practice, it’s common to need two different versions of the same package at the same time. Two libraries often use the same underlying package, such as an HTTP request utility.

But whereas the first library requires features from HTTP version 2.0, the second library is still on version 1.8. Even worse, version 2.0 is backwards incompatible with 1.8. So, by upgrading the HTTP package to solve one dependency, you’ve broken another package in the process. And without any obvious way to foresee or workaround the problem when it comes up.

Composer takes a more limited approach. It installs separate copies of packages for every single project. This allows fine-grained control over the specific versions in use at any time, allows you to upgrade multiple projects at your own schedule, and makes it easy for you to bundle up your code and its dependencies into a self-contained unit. Many people have found this to take less time to manage, despite its duplicative nature.

Composer is not bundled with PHP, so the first step is to download and install it onto your computer. Information about Composer, including the documentation, the latest version of the software, and its issue tracker is at the Composer website.

After Composer is running, you need to find packages to use. Searching and browsing Packagist. After you locate the packages and versions you want, ask Composer to add them.

For example, to install the latest 2.x version of the PHP_CodeCoverage library from PHPUnit:

       % php composer.phar require phpunit/php-code-coverage:2.*

This downloads and installs the package into your project. Then, to let your scripts know where to find PHP_CodeCoverage, or any other package installed by Composer, add this to the top of your script:

       require 'vendor/autoload.php';

That’s it! One single file handles the work to discover where each package and its related classes live.

To find general information on PEAR, read the PEAR manual; to discover the latest PEAR packages, go to The PEAR website also provides links to mailing list archives, as well as RSS feeds that allow easy monitoring of new package releases.

Part of PEAR is a program called pyrus that makes it easy for you to download and install additional PEAR packages. This program is also known as the PEAR installer for PHP 5.3+.1 Recipe 27.4 shows how to use the PEAR installer.

Any developer can use the PEAR class management infrastructure with her projects. When developers create their own packages using the PEAR format, you can use pyrus to download and install the files from each project’s website. This is called creating a PEAR channel. 

Explains how to find a PEAR package that you may want to use and how to install it on your machine. Because PEAR and PEAR channels offer many packages, you need an easy way to browse them. Covers different ways to find PEAR packages. When you’ve found a package’s name and determined which channel server it is on, Shows how to view package details and information.

After you locate a package you want to use, you need to run pyrusto transfer the package to your machine and install it in the correct location. Installing PEAR packages and PECL extensions are the subjects respectively. Shows how to discover if any upgrades are available to packages on your machine and how to install the latest versions. If you want to remove a package.

To view instructions and examples of how to use a particular PEAR package, check the PEAR website. Many packages have end-user documentation complete with examples. The rest typically include at least a set of generated API documentation that provides examples of usage. If all else fails, read the top section of the package’s PHP files; most contain an example of usage there as well.

Documentation for PECL extensions is not always as easy to find. Some PECL extensions are very well documented within the main PHP manual; the APC extension is an excellent example. Other PECL extensions are not documented at all, and usage must be gleaned by reading PHP test scripts included with the source bundles from the PECL website. In extreme cases, you can only get the full idea of what an extension does by reading the extension source code.

The combination of Composer, PEAR, and PECL provides a vast collection of high quality reusable code that make these projects tremendous assets to the PHP community at large.

No comments:

Post a Comment

Post Top Ad