While teaching PHP I mention the term “extension” quite a lot – but I have realized that this may very well be a confusing term for non-PHPers. While most PHP training courses focus on code, I believe getting to know the PHP engine and environment is almost as important as learning to use the language. Extensions are a big and important part of PHP, but they seem to be a big knowledge gap about them and Googling for articles that explain what extensions are, and how they are used and installed, hardly returns good results. So, as part of my attempt to blog about more basic PHP topics, I’ve decided to try and come up with an overview of PHP extensions.
So what are PHP extensions?
An extension in PHP is in fact a module providing some functionality to the PHP Engine. While the term makes it sound like extensions provide some kind of special functionality, in reality many of the language’s most basic functions and classes are provided in extensions. Many extensions are shipped as part of the default PHP distribution, and some are in fact compiled into PHP in such way that they cannot even be unloaded. Come to think of it, perhaps it is best to think of PHP extensions as “language modules”.
Another important aspect of Extensions (in the PHP world) is that like PHP they are written in C (and on rare occasions C++) and are compiled and loaded into PHP as a shared object or DLL, or compiled statically into the PHP binary. This is in contrast with libraries, components and frameworks which in most cases are written and distributed as PHP code.
Some examples you are most likely familiar with are the mysql extension providing all the mysql_* functions, or the gd extension providing image processing functions. Both of these extensions are almost always included with the default PHP distribution. More examples include the session and SPL extensions, which are hardly separable from PHP but are considered extensions because mechanically they use PHP’s internal Extension API.
Most extensions provide new user-space API (PHP functions, classes and constants). Some extensions may not provide new APIs directly, but interact with other extensions or with PHP’s core to provide added functionality – for example, the PDO extension provides new API, but the PDO_MySQL and PDO_OCI extensions do not – they merely add capabilities to the PDO extension.
How are extensions shipped?
As mentioned above, PHP extensions are written in C or C++, which means they are usually shipped as C/C++ source code and sometimes as pre-compiled, OS and CPU architecture specific binaries. Many extensions are shipped with PHP – and if they are not already enabled, all you need to do is enable them in order to use them (more on that later).
There are also many extensions which are not shipped with PHP but are developed in PHP’s official extension repository, called PECL (the PHP Extension Community Library). PECL extensions can be downloaded, compiled, and added to your PHP installation provided that you are running the right version of PHP. This can be done automatically using a command line tool called ‘pecl’, or can be done manually using a set of standard build tools. For Windows users, you may compile extensions manually but pre-built extension DLLs can sometimes be found around the Internet for many of the popular extensions.
If you have the right build tool chain installed, installing an extension using pecl is usually dead simple:
$ pecl install apc
And that’s more or less it. If your extension builds on a 3rd party C library (as do many extensions), you will need to have that library installed as well.
In order to be accepted into PECL, extensions must adhere to certain standards set by the PHP development team. This means you can usually trust PECL extensions to work well, provided that they are actively maintained and are considered stable by their development teams.
For example, the APC, gnupg and sphinx extensions are all relatively popular extensions provided through PECL.
In addition to PECL, PHP extensions may be development outside of PECL – sometimes due to legal / licensing reasons, and sometimes for other reasons. You may find some PHP extensions outside of PECL – but it is recommended to be more cautious with PHP extensions not available through PECL, as these may not adhere to the standards of the PHP development team.
Enabling and Disabling PHP extensions
Once you have an extension installed (this usually mean you have this extension’s DLL or .so file placed in your PHP’s extension directory), you will need to enable it in order to use it.
First, check the output of phpinfo() to see if the extension is not already enabled.
If it is not enabled, you will need to edit your php.ini and make sure that the following is set:
extension_dir=<path to the directory containing your extensions> extension=myext.so
Where “myext.so” is the file name, without the path, of the extension’s .so or DLL file. You will notice your php.ini probably includes multiple extension= lines. This is fine – there should be one for each extension you want to load. You can of course comment out lines referring to extensions you don’t need by adding a semicolon (;) at the beginning of the line.
After making changes to your php.ini, you need to restart your Web server (or your PHP process pool if using FPM / FastCGI) to apply the changes.
To ensure your new extension is loaded, check phpinfo() again. Check your PHP error log (or Web server error log) for any errors during restart which might indicate a problem loading the extension.
For production servers, it is recommended that you disable any extension which you do not use. This may save you a little bit of server memory and CPU cycles, and most of all will reduce the chances of unknown bugs or security issues in those extensions affecting your server.
Keep in mind that some extensions may be build statically into PHP. You will see these listed as extensions in php.ini but you will not be able to disable them, and in most cases you will not see an extension= line referring to them in php.ini or an .so / .DLL files. Removing statically compiled extensions requires recompiling PHP itself, and in most cases this is hardly needed as most statically compiled extensions tend to include core functionality which rarely needs to be removed.
If you are a C/C++ programmer and want to create your own extension, you will need to learn about the Zend Engine 2 API. Unfortunately, there isn’t a lot of documentation on the subject, and what is available as part of the PHP manual has long been outdated.
It is recommended you check out Sara Golemon’s excellent book Extending and Embedding PHP. It has been around for quite a few years now and does not cover the most recent APIs, but it is definitely a good starting point. In addition, you should look for extension creating presentations from recent years PHP conferences – almost every conference has one of these by someone from the PHP Core team. Of course, looking at other extensions source code is always the best place to learn