Extension for PHP or everything will be in Zephir'e

imageProbably every PHP developer (and not just PHP) has a dream - to write his own extension for PHP, which by definition works faster than the interpreted code, while not delving into the intricacies of the Zend Engine, and even better - without knowing C ++. And now the day is nearing when the dream will come true. I want to tell you about a very interesting programming language called Zephir. To whom it is too lazy to read, go to the site and follow the instructions, and I will talk about how to write your extension and about the pitfalls that occur on this very road.

What is Zephir? Zephir is an open-source high-level programming language for quickly and easily creating PHP extensions with emphasis on types (typing) 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 - file package for extension development
  • re2c - regex compiler
  • libjson0-dev - library headers, json-c packages for working with json format
  • libpcre3-dev - headers of 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 in the repository there is a test extension "test" - it will not bother us 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 space in the class and it is also the name of the directory with extension sorts.
  • Classes in the extension must begin with the extension namespace (for example, Test \ TestCase is incorrect)
  • Each namespace must be represented by a directory, and the class is provided with a file with the extension .zep
  • All files and directories of the extension in the love case

Now is the time to try our creation. I remind you that we are still in the root directory of zephir. 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 directory / usr / lib / php5 / 20121212 / (depending on the version of PHP). It must be connected to our PHP. To do this, we will do the following (an example for cli, but you yourself can reproduce these actions for your specific case):
$ sudo nano /etc/php5/mods-available/app.ini

Insert lines
; configuration for php Our Awesome App module
; priority=50
extension=app.so

And link
$ 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 the extension, you may see a message about missing tests. Zephir has the ability to run tests before compilation. You will find sample tests in the unit-tests directory.
PS Cake real
P.SS Please, all comments and amendments to the PM.

Also popular now: