How to Handle Unloaded PHP Extensions at Runtime

    Craig Buckler

    configure PHPUnless you’re creating very simple applications, you will soon require one or more PHP extensions. Extensions are code libraries which extend the core functionality of the language.

    Loading an Extension

    For the purposes of this article, we’ll assume you need to create or manipulate images and require PHP’s GD library. To load the GD library on your system:

    • For Windows, you need to include/uncomment the line extension=php_gd2.dll in your php.ini configuration file.
    • For Linux/Unix, you should use the PHP configure option --with-gd.
    • For Mac OS X … er, you best Google it. Apologies for not providing specific details, but there appear to be multiple ways of configuring GD support. It depends on your version of OS X and whether you’re using the built-in web server or a custom installation of Apache.

    But what happens when you want to move your web application to another host or platform where a different set of extensions are configured?

    Checking an Extension is Loaded

    PHP provides an extension_loaded(name) function which returns true when the named library is available, e.g.

    if (extension_loaded('gd')) {
    	echo 'GD extension is loaded and everything is fine!';
    else {
    	echo 'Where is the GD library?';

    Alternatively, you can check for the existence of specific library function using function_exists(), e.g.

    if (function_exists('gd_info')) {
    	echo 'gd_info() is available so the GD library is probably available.';
    else {
    	echo 'gd_info() cannot be found?';

    However, function_exists() is more risky — another developer could write their own function named ‘gd_info’.

    I’d recommend using function_exists() in situations when a function has been introduced in a later version of PHP. For example, if your application runs in both PHP4 and PHP5, you could check for the existance of imagefilter() (a PHP5-only GD function) before attempting to modify an image.

    Handling Unloaded Extensions

    PHP provides a dl() function for dynamically loading extensions at runtime. Unfortunately:

    • it may be disabled on your system
    • extensions must be loaded by referencing the filename; these are different across platforms
    • it’s a little flaky…
    • so it’s has been deprecated in PHP 5.3
    • and will be removed from PHP6.

    I’d recommend avoiding dl(). That leaves us with three options:

    1. Ensure the extension is enabled on all your target platforms before coding begins!
    2. Alert the administrator when an essential extension is not available during installation. For example, your application should stop gracefully and provide further instructions if it won’t run without the PDO database interface.
    3. Provide a downgraded experience. For example, your application could disable image manipulation if the GD library is not available. You could alert the administrator during installation but still let the application run.

    Do you have any other tips for handling missing PHP extensions?