Php-x Introduction

Source: Internet
Author: User
Tags install php zend

Why PHP extensions are being developed

Php-x is a library for developing PHP extensions. PHP code well written, why to develop PHP extension it?
1, we know that PHP is not good at CPU-intensive operations, then the CPU-intensive related code migration to the extension, you can significantly improve efficiency, such as encryption MCrypt, serialization igbinary, image processing Gd2, and so on.

2, in addition some system-related operations, PHP does not provide interface, then can also be extended to provide, such as multi-process pcntl, multi-threaded pthread, file event inotify.

3, tracking PHP calls, for code optimization advice, such as a variety of monitoring APM.

What Daniel says from user space to kernel space is to change from PHP code to extension.

Languages for developing extensions

PHP interpreter Zendengine provides some column C interfaces that can be developed using the C and C + + languages.
Since the extensions are provided in the form of a dynamic library, it is possible to theoretically generate dynamic libraries and conform to the Zend interface, such as someone using rust to develop PHP extensions.
In addition, you can use Zephir, specifically to develop the language of PHP extension, the famous Phalcon is developed with Zephir.

Developing an extended posture using C + +

Using the API provided by Zend to develop the interface is not really convenient, and the specific process can be seen in this [tutorial] (
Http://www.php-internals.com/book/?p=chapt11/11-02-00-extension-hello-world).

So how do we develop it? Using Php-x, it is an open source Swoole author, encapsulating the Zend API,
You can easily use c\c++ to develop PHP extensions.

Installing PHP7

Php-x only support PHP7 above, here we use PHP7.1 to develop, test found PHP7.2 temporarily not support.
Below we will be in the CENTOS7 environment, you can choose your favorite Linux system.
Since the discovery of the Remi Source has a variety of PHP-related software packages, I became lazy, no longer manually compiled.
1. Install Remi source First, URL
Yum Install-y https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
2. Edit Remi Configuration Vi/etc/yum.repos.d/remi-php71.repo Open PHP7.1

3. Installing PHP and developing PHP's development library
yum install php php-devel -y
4. Installation of CMake, GCC, etc.
yum install cmake3 make gcc gcc-c++ git -y
5. Compiling php-x
Download the Php-x code First, git clone Https://github.com/swoole/PHP-X
Go to Php-x folder

CMake.
Make-j 4
sudo make install

You can see the following output:

Libphpx.so was installed under/usr/local/lib.
6. Testing
Enter the Example/cpp_ext directory
Make install
The generated cpp_ext.so is automatically copied to the PHP extension directory.
You can modify the Php.ini,remi source to use the INI in the PHP.D folder, which is a separate configuration for each extension, which is better than modifying the full php.ini.
We also use this approach.
Look at the configuration directory below:

Then create a new Cpp_ext.ini with the following content:

Extension=cpp_ext.so

Copycp cpp_ext.ini /etc/php.d/

Run php -m | grep cpp_ext If there is an output, the installation succeeds.

If the following error occurs, indicating that the libphpx.so dynamic library is not found, we need to configure the load path of the dynamic library.

Use LDD to see cpp_ext.so, did not find libphpx.so.

Configuring the Dynamic Library load path

    • Look at the environment echo $LD_LIBRARY_PATH variables first, and if the output is empty, configure to export LD_LIBRARY_PATH=/usr/local/lib/ ,
      If it is not empty, theexport LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/
    • Refresh Environment Variablessource ~/.bashrc
    • Refreshing the dynamic library cachelddconfig

More documentation can be found on the author's speech on the Phpcon China 2017 using C + + development PHP7 extension (PS: There are other speakers on the Web page, which is worth a look or download here) as well as official website documentation.

Extended

We see an extension library written using php-x, which does not run independently and requires libphpx.so. Can it be extended independently?

People familiar with c\c++ know that the library is divided into dynamic libraries, and static libraries. If we compile the php-x into a static library and use static links, we can write our own extensions independently of the libphpx.so.

The advantage of independence is that it is easy to deploy and users do not have to rely on php-x after release; the downside is that if you use Php-x, you create code redundancy.

In addition, there are foreign php-cpp like php-x.

Php-x Introduction

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.