In demand


Monday, September 28, 2015

Do You Really Know PHP? Think Again

Many developers believe that they know PHP inside and out.  The truth is they know it outside.  Not many really know about the insides and how to use them.

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.


There 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

Community Efforts


GoPHP7 Extending

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.

  1. Get PHP extensions running “out of the box” when PHP7 is released (no lag time)
  2. Make extensions in general easier to install and use (binaries, ppas)
  3. Get more people involved in extension maintenance (and travis/appveyor running on them all, killing off bugs)
  4. Get more documentation written for extension writing
  5. Codify some of the “best practices” of extension writing and design (a la PSR)

An Inside PHP Online book

This online book is a collaborative effort between several PHP developers to better document and describe how PHP worksinternally.


Extension 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.


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
  • Performance

The solution was to write the API's into PHP extensions that CMS plug-ins could consume as clients. Using php.ini to load pre-compiled binaries for Linux and Windows operating systems. Put simply they want to close source their  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

Skeleton Languages

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.

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. 
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.



PHP is written in C a very low-level language by modern definitions. This means that it has no built-in support for many features that PHP  programmers take for granted, such as reflection, dynamic module loading, bounds checking, threadsafe data management and various useful data structures including linked lists and hash tables. At the same time, C is a common denominator of language support and functionality. Over the years PHP has matured and these concepts become possible. The Zend Engine uses them all. Until now the community has been satisfied in having the Zend Engine remain a black box open to only a few. But the new philosphy is to get everyone involved even at the lower levels of extension development. Thanks to projects like PHP-CPP the community can continue to grow in this area.

Today's Links

Post a Comment

Print this!