Zephir – Build PHP Extensions Without Knowing C
A while back, I mentioned the Phalcon framework in an article. Phalcon is a framework that offers more than most of the current frameworks, but is wrapped up as a PHP extension you install like IMAP, iconv, mcrypt or any other. I won't go into details right now, the links above provide plenty information, but what's important here is the magnitude of the milestone the Phalcon team achieved.
Phalcon is a wonderful hybrid created by wonderful hybrids
Phalcon is a wonderful hybrid created by wonderful hybrids – people who are excellent at both PHP and C. Phalcon is the golden middle ground, a PHP framework that's as low level as it gets (immeasurable performance gains when compared to other frameworks) but its API still high level enough for any experienced PHP developer to use just like in any other framework.
So what is Zephir and what does it have to do with Phalcon? As it turns out, the developers of Phalcon realized pull requests to a C project like Phalcon are coming in far too slowly when compared to the speed of growth in Phalcon's popularity – in no small part due to the rarity of high quality open-source oriented C developers. Fearing a bottleneck, they undertook another adventure that's poised to redefine the world of PHP development as we know it. They decided to build Zephir.
If Phalcon is the golden middle ground between C and PHP, Zephir is platinum. Zephir will be (note I'm saying will be because it's still in very early alpha stages) the only language that lets high level developers develop low level PHP extensions.
Zephir lets high level developers make low level PHP extensions
Zephir will be a static/dynamic typed language that compiles into C. It will have automatic memory management and will enforce strict and explicit over flexible and implicit behavior. The aim is to hide unnecessary C complexity from developers, make the code more debuggable, and produce object oriented libraries that can be used directly in PHP just like any other installable extension. The Phalcon team is currently rewriting the entire Phalcon framework in Zephir, gearing up for version 2.0, which will allow the huge pool of PHP devs using Phalcon to extend it without knowing C.
In addition to being used to develop and maintain Phalcon, Zephir is already in use elsewhere. The Apigility project by the Zend team will be integrating Zephir in order to allow users to improve performance of the APIs built with Apigility. What's more, knowing Zephir will have the additional benefit of allowing you to write your entire web app in it, compile it to C, and install it into your client's servers without exposing your code to anyone. Zephir is taking code protection, PHP app performance and development of PHP extensions to entirely new levels.
Zephir lets you truly close the source of your PHP application, protecting your intellectual property
It's important to note that Zephir is not a replacement for any current web language. It's not a replacement for C either. It's a gateway between C and PHP, allowing you to write high performance PHP extensions and closed-source PHP apps without the headaches and timewastage of a C development cycle. It will neither replicate all of PHP's functionality, nor all of C's functionality – it's not intended as an alternative to either. In fact, not every app should be rewritten with Zephir, as it might turn out to be nothing but a waste of time – the apps that should be rewritten are, for example, those you care about close-sourcing, and those that are computationally too intensive for the PHP interpreter. Rewriting something like WordPress in Zephir, for example, would be a fool's errand because WordPress is a huge yarn of low quality spaghetti code, and more would be gained by rewriting it to proper, modern object oriented PHP5.5.
Are there any code samples? What about Phalcon's performance?
Now, many wonder about the performance impact. If Phalcon is so fast due to it being written in C, won't rewriting it in a higher level language slow it down? Well, yes and no. While it won't allow the developers to dive into the nitty gritty of manual memory space allocation and whatnot, it will allow them to work faster, which will in turn let them iterate more rapidly and improve the design rather than improving only existing implementation. Naturally, some parts will remain written in C simply because there's just no way to rewrite them and keep their quality.
Some initial benchmarks of Phalcon's current implementation versus its Zephir version can be found here.
Trying Zephir today
To try Zephir for yourself (or, if you're an amazing C developer and you'd like to help) you can check out the Github repository. From there, you can also download, build and install the language – just follow the README instructions. Preliminary documentation can be found on the Zephir project home page and their blog is available as well. For those using TextMate or Sublime Text, a syntax highlighter can be found here.
Give Phalcon and Zephir a go, and let us know in the comments below how you like them. If you try out Zephir and develop an extension with it, feel free to drop me a line about turning it into a tutorial.
If you've always wanted to improve PHP on your own, but never took up C properly to do so, Zephir is for you. If you want to protect your code from prying eyes and piracy, just improve performance of your apps, or extend PHP with functionality that can't be effectively replicated through PHP itself, Zephir is for you.
I hope you enojyed this brief overview of this promising new project, and that you're as excited as we are about the benefits it could bring.