When I started to write this article I want to say a few words about PhalconPHP, but maybe I will do it in the future. Probably the whole speed of PhalconPHP framework is from his architecture, of course, it is written well, with all best practices. But unlike other PHP frameworks, Phalcon work’s as an extension which is written in Zephir and finally this post is about Zephir.
Zephir, an open source, high-level language designed to ease the creation and maintainability of extensions for PHP with a focus on type and memory safety.
This is not a tutorial how to start with Zephir, is more like. I found something interesting and I wan’t to do something with it.
Installation
Installation on Ubuntu is very simple – follow this guide.
Important information from this guide is.
Since Zephir is written in PHP you need to have installed a recent version of PHP and it must be available in your console.
Zephir is written in PHP? Right, but Zephir is between PHP and C. It’ is strict like C and but more flexible in an implementation like PHP. Currently, it offers native code generation via compilation to C and a compiler like gcc/clang/vc++ optimizes and compiles the code down to machine code. With Zephir, and it’s very important, you can create extensions for PHP running on the Zend Engine. The name was made from a concatenation of words Zend Engine/PHP/Intermediate.
The code
Code in Zephir must be placed in classes with a namespace. This language is intended to create object-oriented libraries/frameworks, so code out of a class is not allowed:
namespace Test; /** * MyTest (test/mytest.zep) */ class MyTest { public function someMethod() { /* Variables must be declared */ var myArray; int i = 0, length; /* Create an array */ let myArray = ["hello", 0, 100.25, false, null]; /* Count the array into a 'int' variable */ let length = count(myArray); /* Print value types */ while i < length { echo typeof myArray[i], "\n"; let i++; } return myArray; } }
It sims like PHP without $ and with new keyword “let”. Basicaly – this is it.
Once this code is compiled it produce the code that is compiled by gcc/clang/vc++:
Compilation
Following this tutorial I had only one trouble. The php and and phpize must match! This is important.
phpize -v PHP Api Version: 20151012 php -i | grep API xx:PHP API => 20151012
After compilation the output .so file is copied to current php (for cli) extensions directory.
You can import it in your php.ini file and restart your web server.
Life is brutal
The main problem, in my opinion, is with IDE when we write an extension in Zephir – currently, there is no good IDE for Zephir. OK, we can us C++ highlighting and it not so bad. But when we do some mistake, Zephir compiler is brutal and only says:
zephir build Zephir\Exyour aception: Cannot parse file: /var/www/utils/utils/greeting.zep
It’s hard to write the code in Zephire. But, if you want to speed up your application or you want to save the memory – in my opinion, it is the simplest, best, way to write PHP extension.
PHP benefits
We have access to all PHP super globals like $_SERVER, $_GET, $_REQUEST, $_POST so we can do exactly the same functionality in clean PHP and Zephire but in Zephire we don’t use $ so equivalents for above are _SERVER, _GET, _REQUEST, _POST. Also, all php functions are allowed and we have access to PHP objects (we must declare it with var and set it wit “let”).
How to use?
The problem with using our new extension is simple – no IntelliSense. This is a big problem, we have all code in one extension file and our IDE can’t see what methods are in our new object :( The solution is very simple.
zephir stubs Generating stubs...
It generates mock-up classes which are implemented in our extension so we can use it in our dev environment. IntelliSense is back