Extending PHP

The base of the PHP language is very simple, having just enough to set and retrieve variables, work with loops, and check whether a statement is true or not. The real power behind PHP comes with its extensions - add-ons to the base language that give it more flexibility. There are hundreds of extensions to PHP, and they can be broken down into five distinct types: core, bundled, PECL, third party, and DIY.

  • Core extensions are extensions that are bundled with PHP itself, and enabled by default. For all intents and purposes they are part of the base language, because, unless you explicitly disable them (few people do, and sometimes you cannot), they are available inside PHP. For example, the mechanism to handle reading and saving files in PHP is actually handled by an extension that is automatically compiled into PHP.

  • Bundled extensions are extensions that are bundled with PHP, but not enabled by default. These are commonly used, which is why they are bundled, but they are not available to you unless you specifically enable them. For example, the mechanism to handle graphics creation and editing is handled by an extension that is bundled with PHP, but not enabled by default.

  • PECL stands for "PHP Extension Community Library", and is as a subset of the PHP Extension and Application Repository, PEAR. PECL (pronounced "pickle") was originally created as a place where rarely used or dormant bundled extensions could be moved if they were no longer considered relevant. PECL has grown a lot since its founding, and is now the home of many interesting and experimental extensions that are not quite important enough to be bundled directly with PHP.

  • Third-party extensions are written by programmers like you who wanted to solve a particular problem that was unsolvable without them creating a new extension. There is a variety of third-party extensions available out there, with the sole difference between a third-party extension and a PECL extension is that there are various rules about having code being submitted to PECL. Third-party extensions are frequently unstable, and often just downright crazy. That is not to say they are bad - give them a shot and see what you can do.

  • Finally, Do-It-Yourself (DIY) extensions are simply extensions you created yourself. PHP has a remarkably rich extension creation system that makes it quite simple to add your own code as long as you know C. Later on we'll be going through the task of creating your first extension from start to finish. Note that creating your own extension requires that you have the ability to compile PHP - this is not a problem if you use Unix, but is very difficult for Windows users without the correct software.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: PEAR >>

Previous chapter: Selling PHP to your boss

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.