
Probably every PHP developer (and not only PHP) has a dream - to write his own extension for PHP, which by definition works faster than interpreted code, without delving into the subtleties of the Zend Engine, and even better without knowing C ++. And that day is nearing when the dream will come true. I want to tell you about a very interesting programming language called Zephir. Who is too lazy to read, go to the
site and follow the instructions, and I will tell you how to write your extension and about the pitfalls that are encountered on this way.
What is Zephir? Zephir is a high-level open source programming language for quickly and easily creating an extension for PHP with an emphasis on types (typed) and security. Its syntax is very similar to PHP itself.
So, let's begin. First we need to install the missing components:
$ sudo apt-get install php5-dev re2c libjson0-dev libpcre3-dev
- php5-dev - package with files for developing extensions
- re2c - regular expression compiler
- libjson0-dev - library headers, json-c package for working with json format
- libpcre3-dev - headers for a library that provides functions for working with regular expressions
Then make a clone from the repository:
$ git clone https://github.com/phalcon/zephir.git
The version in the repository is not always stable, I used commit 6eda047f512b6600cf8afc79348381c64198fcb5.
Go to the directory with Zephir and configure everything for your extension. Now there is a test extension “test” in the repository - it won't bother us too much.
$ cd zephir $ cp -r ext/ .ext/ $ mkdir app
create a class version of our extension
$ nano app/version.zep
namespace App; class Version { public static function get() { return "0.0.1 super beta"; } }
and tweak the config.json configuration file
$ nano config.json
{"namespace":"app"}
Attention!- The namespace in the config is the namespace namekeys in the class and it is the name of the directory with extension sorts.
- Classes in an extension must begin with a namespace extension (for example, Test \ TestCase is incorrect)
- Each namespace must be represented by a directory, and the class is represented by a file with a .zep extension .
- All the files and directories of the expansion in the lovecourse
Now is the time to try our creation. Remember that we are still in the zephir root directory. Now you need to deploy zephir, and then compile our extension.
$ sudo ./install $ sudo ./bin/zephir compile
As a result, we will have the app.so library in the / usr / lib / php5 / 20121212 / directory (depending on the PHP version). It needs to be connected to our PHP. To do this, we will do the following (an example for cli, but you yourself will be able to reproduce these actions for your particular case):
$ sudo nano /etc/php5/mods-available/app.ini
Insert rows
; configuration for php Our Awesome App module ; priority=50 extension=app.so
And let's zink
$ sudo ln -s ../../mods-available/app.ini /etc/php5/cli/conf.d/50-app.ini
Now it's time to check out our first extension:
$ php -r "echo App\Version::get() . PHP_EOL;"
And we see these wonderful lines:
0.0.1 super beta
While compiling an extension, you may see a message about missing tests. Zephir has the ability to run tests before compiling. Examples of tests can be found in the unit-tests directory.
PS Cake real
PSS Please all comments and amendments to the PM.