Writing PHP Extensions
Knowing how to use and write PHP extensions is a critical PHP development skill that can save significant time and enable you to quickly add new features to your apps. For example, today, there are more than 150 extensions from the PHP community that provide ready-to-go compiled libraries that enable functions. By using them in your apps, you can avoid developing them yourself.
Despite the large number of existing PHP extensions, you may need to write your own. To help you do that, this document describes how to:
- Setup a Linux PHP build environment.
- Generate an extension skeleton.
- Build and install a PHP extension.
- Rebuild extensions for production.
- Understand extension skeleton file content.
- Run extension tests.
- Add new functionality (functions, callbacks, constants, global variables, and configuration directives).
- Use basic PHP structures, including the API.
- Use PHP arrays.
- Catch memory leaks.
- Manage memory.
- Use PHP references.
- Use copy on write.
- Use PHP classes and objects.
- Use object-oriented programming (OOP) in an extension.
- Embed C data in PHP objects.
- Override object handlers.
- Avoid common issues with external library linking, naming conventions, and PHP resource type.
To help you learn from all the coding examples in this document, please visit the GIT repository, https://github.com/dstogov/php-extension. It includes a copy of all the files generated when creating the sample extension described in this digital book. The extension modifications are reflected by separate GIT commits.