Image-Zend-What-Are-PHP-Extensions
March 31, 2020

How to Use PHP Extensions

PHP Development
Modernization

In this blog, we give an introduction to PHP extensions for beginning PHP developers. Then we give more in-depth definitions for developers familiar with PHP extensions. 

What Is A PHP Extension?

A PHP extension is a specially formed library or plug-in that provides a function that can be used by many applications.

Why Use PHP Extensions?

By using extensions, you can avoid having to recreate the same code for numerous applications. To use an extension, you simply connect it to your application’s PHP binary. 

You can make use of a publicly available extension or create your own to: 

  • Provide functions and/or classes to work with data structures such as strings, arrays, objects, lists, and graphs.
  • Implement bindings to some external libraries, such as POSIX regular expressions or simplexml.
  • Connect to databases, such as MySQL or PostgreSQL.
  • Increase security using Hashing or data encryption via technologies such as OpenSSL and Sodium.
  • Facilitate processes with external services via technologies such as XML-RPC and SOAP.
  • Add new capabilities to your application including caching, debugging, profiling, and testing.

PHP extensions are usually written in C and then compiled to native machine code. However, PHP 7.4 introduced Foreign Function Interface (PHP FFI), which enables PHP developers to write extensions and bindings to C libraries in pure PHP. 

What Is a .PHP File Extension?

The term .php file extension refers to the name of a file with a PHP script or source code that has a ".PHP" extension at the end of it. It's similar to a Word file with a .doc file extension.

Are PHP Extensions the Same Thing as .PHP File Extensions?

PHP extensions are not the same thing as a .php file extension or PHP class extension (inherited classes). A PHP extension is a specially formed library or plugin. But a .PHP file extension is the name of a file. And a PHP class extension refers to a class that extends another.

In other words, files that have a .php file extension are not the same thing as an extension a PHP developer uses in code to extend functionality. 

Where Do I Find Shared PHP Extensions?

If you have developed any code in PHP, you have already used extensions. Almost all the functions and classes provided by PHP come from different extensions. One of the simplest examples is the  function strlen() implemented in “standard” extension. Another example is “mysqli_connect,” which is implemented in the “mysqli” extension.

If your application needs some new functionality that is potentially useful for other applications, you should consider using a reusable library. You can add it by:

  • Implementing this functionality in pure PHP, by writing one or more PHP classes.
  • Using a ready-made extension that provides the functionality. 
  • Writing your own.

PHP Extensions That Come With PHP

If you are lucky, you will find an extension that already provides the feature. There is a rich set of extensions maintained and distributed with PHP. For example, PHP 7.4 provides about 70 different extensions. Some of them are always compiled and linked directly into the PHP binary, like “standard.” These extensions are available out of the box. 

Other extensions are optional. Oftentimes, these are distributed as dynamic plug-ins that should be loaded though the php.ini extension directive — for example, extension=mysqli.so. 

Third-Party PHP Extensions

Despite the extensions that come with the PHP core, you can find a lot of third-party extensions on the Internet. One of the biggest repositories — https://pecl.php.net/packages.php — contains almost 400 extensions. Not all of them are stable, portable among different PHP versions, and/or well supported. If you see an extension that could be useful, you need to try it to determine if it meets your requirements.

How to Use PHP Extensions

Because existing extensions don’t cover all the application domains, from time to time, you will need to develop a new extension. Companies that do business with PHP often develop extensions with very specific functionality. They also use extensions to re-implement mission-critical parts of code in C, to achieve better performance. 

Learn more about PHP extensions in our technical guide, including:

  • How to prepare a development environment.
  • How to generate an extension skeleton. 
  • How to install PHP extensions.
  • What basic PHP internals are.
  • Using advanced techniques to create PHP object and classes with custom behavior.

PHP Extensions Technical Guide

Develop Better PHP With Zend

PHP extensions are just one aspect of PHP development. There's a lot else to consider, like debugging, security, deployment, performance, and integrations. PHP training can help you learn more. 

But using the right PHP development tools will help you development better applications. One of the best tools to use is Zend Server.

Zend Server is a PHP application server. You can use Zend Server to:

  • Improve debugging.
  • Boost security.
  • Streamline deployments.
  • Accelerate performance.
  • Simplify integrations.

See for yourself how Zend Server will help you. Get started with a free 30-day trial.

Try Zend Server

 

Additional Resources