Surpassing it's simple roots today's PHP is now offering a well-defined and documented way for writing custom extensions in C language. Extensions are the surest way for improving execution speed, securing valued intellectual property and guarding a market advantage. Extending the PHP language itself via compiled language libraries is the future of using PHP in business sensitive situations. Using extensions make PHP a true competitor that can meet the challenges of software development for the web.
Learning the Insides
Regardless of its popularity, no written specification or standard existed for the PHP language until 2014, leaving the canonical PHP interpreter as a de facto standard.
Since 2014, there has been ongoing work on creating a formal PHP specification. This of course made it difficult to dissect and learn about the PHP language internals without the help of those who work closely with the core.
BooksThere is little documentation on how to extend PHP. Developers seeking to build PHP extensions and increase the performance and functionality of their PHP applications are left to word of mouth and muddling through PHP internals without systematic, helpful guidance.
Although the basics of extension writing are fairly easy to grasp, the more advanced features have a tougher learning curve that can be very difficult to overcome. There are books about the PHP internals written for programmers to be able to understand them and write their own extensions.
- Extending and Embedding PHP by Sara Golemon
- Advanced PHP Programming by George Schlossnagle.Teaches every PHP developer how to increase the performance and functionality of PHP- based websites, programs and applications.
- Building Custom PHP Extensions by Blake Schwendiman. Includes object-oriented examples and methods for exposing new internal classes.This book is intended for advanced PHP and C programmers looking to either extend PHP directly or to gain a better understanding of the PHP
PHP7 is coming and it’s really really fast! The architectural changes in PHP7 mean that every PHP extension will need updating, and some of them both core and third party are already rather neglected and without maintainers. The goal of PHP7 Extended is to make extensions first class citizens of the PHP community.
- Get PHP extensions running “out of the box” when PHP7 is released (no lag time)
- Make extensions in general easier to install and use (binaries, ppas)
- Get more people involved in extension maintenance (and travis/appveyor running on them all, killing off bugs)
- Get more documentation written for extension writing
- Codify some of the “best practices” of extension writing and design (a la PSR)
This online book is a collaborative effort between several PHP developers to better document and describe how PHP worksinternally.
An Inside PHP Online book
TutorialsExtension writing is tough. You’re delving into a new area of PHP programming and trying to grasp another programming language. To get anywhere you’re going to need some guidance. Unfortunately, not all tutorials are made equal. That’s why it’s so important that you’re able to discern between tutorials that will actually benefit you and those that will only waste your time. Walking through a proper tutorial can be the difference between stress-free learning or giving up in frustration.
- The Internals section of the PHP manual
- Kristina Chodorow’s PHP Extensions Made Eldrich on her blog in 2011:
- Sara Golemon’s Zend DevZone articles with parts I, II and III (2005, 2006):
Closed Source Solutions
Why learn all this you might be asking yourself. In the last years before retiring from Drupal and Wordpress development I had three medium sized clients that wanted the same thing. They wanted to extend the CMS with proprietary code for games and medical applications. This is the strongest argument against Open Source development being the best strategy for everything and everyone. Some start-ups need to protect their intellectual property in order to gain funding or become competitive. Using PHP's extensibility for development of protected source software has the following benefits
- Security - in the form of closed source.
- Control - absolute control over usage.
- Distribution - distribution of products through packaged and embedded software
The advantage of closed-source software is that you can embed your intellectual property in it and minimize the risk of competition. If you develop a truly innovative product or algorithm and deploy it as open source, it's easy for a competitor to build a knock-off immediately and gain reputation, market share, etc. Release the same product as closed-source, and you've gained some time to become the leader while your competition struggles to catch up. Without use of compiled code any advantages gained by using PHP might be lost.
Frameworks Based on PHP Extensions
Modern PHP frameworks share a common problem. They are slow. To fix this they have take a year or two off where they would loose their market share. Now they rely on the PHP interpreter and caching systems to boost their flagging performance. Some outliers however have taken up the guantlet and chosen to use PHP's extensibility as the solution.
A full-stack PHP framework delivered as a C-extension.It's innovative architecture makes Phalcon one of the fastest PHP frameworks.
The Yet Another Framework (Yaf) extension is a PHP framework that is used to develop web applications.
One of the drawbacks of implementing a Framework as a PHP Extension is that they are not easily maintainable as all work has to be done in C language. There are some projects though that act as intermediaries between the ease of PHP and its C language syntax and the writting of actual C language for the Zend Engine to extend it.
Zephir is a high level intermediary that tries to ease the building and maintenance of extensions for PHP. Zephir extensions are exported to C code that can be compiled and optimized by major C compilers such as gcc/clang/vc++. Functionality is exposed to the PHP language.
For those that do not want to learn C, but want to get a taste of building their own extensions, there is Zephir. It was born out of an extension-based framework need for easy maintenance, and it sits in between the C Language and Zend Engine extension framework.
The PHP-CPP library has encapsulated all these complicated structures in very easy to use C++ classes and objects. You can write amazingly fast algorithms using C++ without ever having to do a direct call to the Zend engine, and without even having to look at the Zend engine source. With PHP-CPP you can write native code, without having to deal with the internals of PHP.
PHP-CPP was initiated by Emiel Bruijntjes from Copernica BV, a company with a large PHP code base. They always felt the desire to move parts of their code base to a native implementation, but postponed this because writing native C extensions was simply too difficult. Although it could be done, it would result in code that would be too complex to maintain in an enterprise environment - and that would be next to impossible to hand over to colleagues. They also once believed that if they waited long enough, one day the PHP core developers would fix their API and come up with a better way to build native integrations. This waiting lasted until the fall of 2013. In the fall of 2013 we realized that no one else was going to build the PHP-C++ integration, and came up with a normal, decent and simple API.Unlike regular PHP extensions - which are really hard to implement and require a deep knowledge of the Zend engine and pointer manipulation - extensions built using PHP-CPP are not difficult to develop at all.