DEV Community

Anders Björkland
Anders Björkland

Posted on

Extending PHP - why not a library instead?

Just the Gist

Extensions are global "libraries" that provide a specific functionality to PHP, such as MySQL-support or image manipulation.

Extensions are almost not libraries

Extensions are plugins for the PHP language that provides a specific feature or functionality. There are many different extensions, some are bundled-extensions that are included with the PHP-language package and some are third-party extensions you can download from either PECL or their specific vendors. Example of what these extensions can bring are:

If you are using any PHP frameworks, you are probably already using one of these extensions. But if you are not, you can download them from PECL or their specific vendors - but most likely you have some in your php/ext folder and have to activate them in the php.ini-file. Active extensions are not limited to just the current project you are working on. They will be available for all projects where you are using the same PHP environment. This may seem like a lot of extra-resources brought in where they might not be needed. So why are these extensions not libraries instead, that can be brought in on project-basis?

The rationale

Something that distinguishes extensions from libraries are how they are implemented. Extensions are written in C (or C++) while libraries are written in PHP. An extension may be using C functions that are not available in PHP, but primarily it will communicate directly with the driving force behind the PHP-language: the Zend Engine. This means that aside from having access directly to C functions, it gets a speed boost.

What about you?

Is extensions confusing, or do they make a lot of sense? What benefits do you see of using extensions and not libraries? Would you rather see some extensions being libraries instead? Comment below and let us know what you think ✍

Further Reading

Discussion (0)