Create PHP7 Extension, The Easy Way!

Mohamed Fawzy
tajawal
Published in
4 min readMay 8, 2018

Introduction:

PHP extensions are compiled libraries which enable specific function to be used in your php code ( mainly php extension wrote using C ) .

For example , you need to work with SQLite3 using PHP , you can implement your own methods and functionalities to connect to Sqlite3, make queries to DB from your application level , However that’s not a trivial task , Plug that is not only your own requirements , but other developers need to do similar thing .

So someone had developed it and shipped it as php extension you can install this extension and enable it inside php.ini using the following extension=sqllite3.so and that’s it you have access to all sqlite3 functionalities through your application level which is php in this case .

Your First Extension

Zephir:

According to zephir official documentation you can define it as the following

an open source, high-level/domain specific language designed to ease the creation and maintainability of extensions for PHP with a focus on type and memory safety.

Features:

  • Type system: dynamic/static .
  • Memory Safety: pointers or direct memory management aren’t allowed .
  • Compilation model: ahead of time .
  • Memory model: task-local garbage collection .

How it works ?

You write php code and your code compiled as c extension and finally you can add it to your php.ini file .

Here’s example of Compilation Scheme:

Compilation Scheme
  • Let’s go by example for Hello world extension .

Zephir Install

Requirements :

  • gcc >= 4.x/clang >= 3.x
  • re2c 0.13 or later
  • gnu make 3.81 or later
  • autoconf 2.31 or later
  • automake 1.14 or later
  • libpcre3
  • php development headers and tools
  • re2c
  • php-zephir-parser

If you’re using Ubuntu, you can install the required packages this way:

sudo apt-get update
sudo apt-get install git gcc make re2c php7.0 php7.0-json php7.0-dev libpcre3-dev

Since Zephir is written in PHP you need to have installed a recent version of PHP and it must be available in your console:

Also, make sure you have also the PHP development libraries installed along with your PHP installation:

Steps

git clone https://github.com/phalcon/zephir

cd zephir

./install -c

Verify your installation

zephir help

If everything is well, you should see the following help in your screen:

Extension skeleton :

zephir init helloworld

After this, a directory called “helloworld” is created on the current working directory:

extension directory structure
  • ext: contains the code that is going to be used by the compiler to produce the extension .
  • helloworld: this directory has the same as our extension. We will place Zephir code in this directory.
  • config.json: this file contains configuration settings we can use to alter the behavior of Zephir and/or this extension.

Adding Our First Class:

Inside helloworld directory .

Zephir is designed to generate object-oriented extensions. To start developing functionality we need to add our first class to the extension.

lets create helloworld class inside our extension to render hello world

The code for this class must be placed in “helloworld/helloworld/greeting.zep”:

Now, we need to tell Zephir that our project must be compiled and the extension generated:

zephir build

Initially, and only for the first time, a number of internal commands are executed producing the necessary code and configurations to export this class to the PHP extension, if everything goes well you will see the following message at the end of the output:

zephir build output

Installation Verifications:

Testing your extension from PHP:

Run the following command php zephir_helloworld.php your output should be the following Hello World!

Conclusion:

If you’re software engineer , php developer need to develop and play around with how php works and how you can build your own extension i suggest zephir has a very powerful documentationa and felxability also it’s memory safe management . however due this it has some limitations according to the following Zephir was designed to be safe, so it doesn’t implement pointers or manual memory management, so if you’re a C programmer, you will feel Zephir less powerful but more friendly than C.

References :

--

--

Mohamed Fawzy
tajawal

Traveler in space and time, Part-time Researcher in Deep learning, High-performance computing, Software architecture.