PHP extension Development (Zephir version)

Source: Internet
Author: User
Tags automake

The previous article introduced the C language method of developing PHP extensions, and now describes how to develop extensions using Zephir.

About Zephir need a brief introduction: Zephir is a high-level language for PHP developers who can write a compile/static type. Is a good Phalcon team for the development of the second version of the framework written by the emerging language, its syntax elegant, development and extension is very convenient, Execution efficiency is said to be the same as C, his compilation process is as follows: Zephir-C, bin.

The purpose of Zephir is to create an object-oriented library or framework, so it differs from the clang extension and cannot write non-OOP modules.

If you want to know more, please visit our website: http://zephir-lang.com/

Installation of Zephir

Prerequisites for installing Zephir:
Building a PHP extension and using Zephir requires the following 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


If you are using Ubuntu, you can install packages like this:

$ sudo apt-get  update$ sudo apt-get install git gcc make re2c php5 php5-json Php5-dev libpcre3- Dev

Also, make sure that you also have the PHP development library installed (when PHP is installed):

$ phpize-for:P HP API Version:         20121113Zend Module Api No:        20121212

Currently the Zephir compiler must be cloned from GitHub:

$ git clone https://Github.com/phalcon/zephir

Run the Zephir Setup program (compile/create parser):

$ cd zephir$. /install-json$. /install-c

You may also need to install Automake before you install the Mac:

$ Brew Install Automake

Detects the installation and checks whether it can be performed from any directory Zephir:

$ Zephir Help _____              __    _/__  /  ___  ____  //_  (_) ____  //  /_/ __ \ __ \/___///__/  __//_////////____/\___/. ___/_//_/_/_/         /_/0.5. 9a.. .

Developing extensions with Zephir

The first thing we're going to do is build an extension framework that will provide us with the basic structure of the extension
In the following example, we created an extension called "Utils":

$ Zephir Init utils

After that, there is a "utils" directory under the current directory:
utils/
ext/
utils/
The ext/directory contains the code required by the compiler to generate the extension (C source).
utils/Directory is our Zephir source code.

We need to cut to the working directory, which is utils, to start writing code:

$ cd utils$ lsext/Utils/config.json

Among the Config.json, this file contains the configuration that can be used to change the behavior of Zephir.
Add our First class:

// Utils/utils/greeting.zep namespace Utils; class greeting{    publicstatic  function say ()    {        "Hello world! " ;    }}

Now, you need to tell Zephir to compile our project into an extension, which is done in the project directory:

$ Zephir Build

If successful, the following information will be printed:
...
Extension installed!
Add extension=utils.so to your php.ini
Don ' t forget to restart your Web server

Initial test:
$ php-m
[PHP Modules]
....
Spl
Standard
Tokenizer
Utils
....

OK, we use PHP to invoke:


<? Phpecho Utils\greeting::say (), "\ n";

Print Hello world! It worked!

PHP extension Development (Zephir version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.