By Craig Buckler

How to Handle Unloaded PHP Extensions at Runtime

By 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?

  • I’d also add class handling functions. PHP is very flexible with those. You have class_exists(), interface_exists(), method_exists(), property_exists() and others. Really useful if your app (ab)uses classes.

  • Another tip for missing PHP extensions?

    …Just move to ASP.NET :-)

  • Michael Gauthier

    One option is to PEAR package your code and add dependencies on the required extensions. Then the code will warn you or refuse to install if the extensions are missing.

  • Tim

    @Michael Gauthier

    You still need access to run the installer. Most hosts wont give you that flexibility

    I have this same problem at the moment in that I need some kind of compression extension enabled, and phar is the only thing that is enabled by default, BUT not until 5.3.x becomes mainstream. Still stuck in the land of deploying code to 4.x servers at times :( Joys of CMS publishing.

  • Anonymous

    Yeah, I like the guy 2 up from me. Spot on bro, ASP.NET is the superior technology.

  • markfiend

    I feel Tim’s pain, I also still have to deploy php4 code at times.

  • cyphix

    I’d rather poke my eyes out with hot lead than use ASP.NET ;)

  • The above comments regarding ASP.NET were made without any explanation or justification and are thus meaningless. Please confine comments to the subject at hand.

  • cyphix

    I could say the same for the people who posted anti-PHP comments….. ;)

  • Atul

    Not much informative.

  • Payal

    Nice post and informative too…..!
    While searching about PHP i got a site having good material with presentations on PHP , it is
    I am looking to have more good stuff about PHP extension topic from your side.

Get the latest in Front-end, once a week, for free.