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:
- Connect to MySQL, PostgreSQL, SQLite (and more) databases.
- Multibyte String support.
- Internationalization (intl) for locale aware operations.
- Graphics Draw (gd) for creating and manipulating images.
- ...and so much more.
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?
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 ✍
- StackOverflow on when to use extensions: https://stackoverflow.com/questions/1567605/php-extension-vs-library-vs-class-when-and-why
- StackOverflow on converting between library and extensions: https://stackoverflow.com/questions/4960450/php-extension-vs-library-and-can-it-be-converted
- A list of bundled extensions: https://www.php.net/manual/en/extensions.membership.php#extensions.membership.bundled
Top comments (0)