On the PHP extension development

What is PHP extension

Popular said, PHP extension is to enhance the PHP language function plug-ins. PHP provides the syntax of programming languages, such as branching, looping, functions, classes, etc., which are provided by PHP itself. In some cases you need to be based on the PHP language extensions, data structures and interfaces provided by then we need to develop the underlying PHP PHP extensions, so to supplement or extend PHP language to make it more powerful. Of course, PHP itself has integrated some basic, powerful, excellent PHP extension.

 

 

PHP extension benefits

From the above understanding that, PHP extensions PHP extensions may be up in the basis of the original PHP, making it more powerful. On the other hand, PHP extensions can manage and maintain PHP functions by way of "plug-in", if all of the features integrated into the PHP language, PHP would have been too bloated, and because not flexible enough. And with the expansion, to solve this problem.

 

 

PHP extensions of the form

Under Linux, PHP extensions to .so file exists, exists in .dll files under Windows.

 

 

When to use PHP extension

Alone in PHP and can not meet all development, such as when using Redis or MongoDB in your project, you need the appropriate PHP extension, to enhance the PHP language, so that PHP can be operated Redis or MongoDB, to complete the powerful more functional items .

 

 

When developing your own PHP extension

Develop your own PHP extension is necessary it? It is not. Typically, as a PHPer you do not need to develop their own PHP extension, but in some cases it may be necessary to develop PHP extensions. For example, I encountered such a problem. The third party provides the dynamic link library files (.dll files, non-COM DLL files) under Windows, but I did not find how to load the DLL file to call in PHP mode, so I need to write a PHP extension by PHP extensions to load and call DLL files provided by third parties.

 

 

Develop your own PHP extension

I have already mentioned in front of my problems, third parties provide a dynamic link library files under Windows, but I have no way to load and call DLL files under PHP is found, it was decided to write your own PHP extension to load and export function provided by a third-party DLL file called in. The following is my record to setup the development environment and compile PHP extensions PHP extensions source.

 

Preparation Tool

First point, DLL files can only run on a Windows system, Linux system is unable to run. Well, we do not consider this extension is the Linux system, the system can only consider Windows. Therefore, the development tool is ready to  VS2015 . At first, I found some information on the Internet, a lot of information are written, developed CygWin PHP extension needs to be installed on Windows, through my own learning, I can tell you " do not need ." Of course, our extension runs only on Windows systems, if you need to run on a Linux system, the need for Cygwin I do not know. Of course, other versions of VS should also be possible, but I only tested VS2015 nothing.

 

Download PHP source code

In addition to the need to install VS2015, also you need to download PHP source code, I downloaded the source code is PHP 7.2 source code.

Download PHP source address is: https://windows.php.net/download/

 

After opening the address to download the source code as shown in FIG:

 

Here, download PHP 7.2 source code, and note with the words of a VC15 x64 in the source below.

 

The downloaded file is: php-7.2.20-src.zip

After downloading the source code directory to decompress, to decompress: PHP-7.2.20-src

 

Download the source code is not a complicated matter, but there are several versions of the page source code is available for download, which is sometimes more tangled problem selection, php7.2 here to download the source code, because I was php7.2 local use environment of.


Creating Extended

Enter php-7.2.20-src \ ext directory under, there is a file named ext_skel_win32.php in this directory, execute the command line:

1 php ext_skel_win32.php --extname=loaddll

Which, LoadDLL is the name of the extension to be created . Want to successfully execute the command, you need to add the PHP executable program into the environment variable.

 

Implementation of the following:

λ php ext_skel_win32.php --extname=loaddll
Creating directory loaddll
FIND: Parameter format not correct
chmod: missing operand after ‘644’
Try 'chmod --help' for more information.
FIND: Parameter format not correct
chmod: missing operand after ‘755’
Try 'chmod --help' for more information.
Creating basic files: config.m4 config.w32 .gitignore loaddll.c php_loaddll.h CREDITS EXPERIMENTAL tests/001.phpt loaddll.php [done].


To use your new extension, you will have to execute the following steps:


1.  $ cd ..
2.  $ vi ext/loaddll/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-loaddll
5.  $ make
6.  $ ./sapi/cli/php -f ext/loaddll/loaddll.php
7.  $ vi ext/loaddll/loaddll.c
8.  $ make


Repeat steps 3-6 until you are satisfied with ext/loaddll/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.

See above output tips, then we create a PHP extension to generate success. The above output is as follows:

At this time, \ ext directory created under the loaddll the directory, which is generated out of the PHP extension templates can be developed on the basis of the template.

 

Create an extended project using VS2015

Open VS2015, I used here is the VS2015, other development environment was not tested.

Select "File" in VS2015 -> "New" -> "Create project from existing code," to create a PHP extension solutions, as shown below:

After you select the "Create project from existing code," there will be created by the project wizard, as shown below:

Click on "Next" to the following figure:

In this step, select the directory just generated expansion modules, and then fill in the project name, here is the "loaddll", click "next" to reach the figure below:

This step DLL project selection, click Next to arrive in the following figure:

Click on "Next" to reach the figure below:

Click "Finish" and wait for VS2015 began to create the project.

 

Configure compilation options and compile the project

VS2015 after generating the project is completed, the project switching "Release" and "x64" option, as shown below:

After the handover is completed, the item in the right select "Properties", as shown below:

To modify the configuration type DLL, the extension object file thus generated automatically become .dll, as shown below:

Select "Configuration Properties" -> "C / C ++" -> "General", click on the "Edit" in the "Additional comprising Directory" to add the relevant directory, as shown below:

It should be included in the directory includes the several directories below:

Here is the addition of a few main PHP source directory, because the need to compile PHP extension PHP source code underlying kernel data structures are supported, and therefore needs to include more than a directory.

 

Select "Configuration Properties" -> "C / C ++" -> "preprocessor" click "Preprocessor definitions" in the "Edit" preprocessing directives related to add, as shown below:

In the pretreatment, HAVE_LOADDLL in LOADDLL is an extension of the name, COMPILE_DL_LOADDLL in LOADDLL also the name of the extension, the name of the name when the best start generating extension template should be consistent.

 

Compile the source code

The php-7.2.20-src \ copy config.w32.h.in under win32 \ build \ directory to php-7.2.20-src \ main \ directory, rename it to config.w32.h, this .h file at compile time is needed, but in php-7.2.20-src \ main \ file the case and did not therefore need to manually replicate.

 

Add the following code in the config.w32.h

1 #define PHP_COMPILER_ID "VC15" 

Note here VC15, download the source code, we have seen that identifies.

 

Next, copy the PHP installation directory files to loaddll php7ts.lib directory, it is noted from PHP installation, rather than PHP source directory.

 

php7ts.lib in the directory php7.2.10 \ dev directory, I use a php7.2.10 under wamp64, we all use the corresponding .lib file can, of course, this .lib file must also be php7.2 because we downloaded the php7.2 source.

 

Add in the resource Files project php7ts.lib file, add the file is relatively simple, the same right to add.

 

Press F7 generation solutions as:

See "a success" file later, in the directory \ php-7.2.20-src \ ext \ loaddll \ x64 \ Release loaddll.dll will generate a file that is our PHP extension files.

 

PHP extensions installed and tested

The VS2015 generate loaddll.dll rename php_loaddll.dll, copy it to expand PHP environment, my path is php \ php7.2.10 \ ext, must be familiar with PHP PHP extensions will be added. As shown below:

Increase in the php.ini configuration file, as shown below:

Using php -m to see if there loaddll.dll module, as shown below:

phpinfo view, as shown below:

As can be seen from the above, our PHP extension has been properly installed, then you need to test whether we can extend the run.

 

In PHP source directory php-7.2.20-src \ ext \ loaddll \ loaddll.php have a test file, the execution of the command at the command line:

1 php loaddll.php

Output as follows:

1 λ php loaddll.php
2 Functions available in the test extension:
3 confirm_loaddll_compiled
4 
5 Congratulations! You have successfully modified ext/loaddll/config.m4. Module loaddll is now compiled into PHP.

As shown below:

See above output, indicating that the extension of the template compilation is successful, we can continue to develop the actual expansion modules.

 

to sum up

In many cases, the first step is to build learning programming environment, and the process often built environment due to the step is too complicated, and the information nor detailed steps fool, leading many programmers who want to learn or even the first step is to get started You can not step out. For the development of PHP extension and is also the same.

How to make PHP applications through PHP extension DLL function can be called directly, in addition to master the knowledge of the C language, it also requires knowledge of Windows programming, this would not be discussed, because there is knowledge of the C language and Windows programs design knowledge, call the DLL is not difficult in PHP extension.

I hope this article to be helpful!

 


 

Welcome to the concern micro-channel public number: "Code farming UP2U"

 

Guess you like

Origin www.cnblogs.com/tosser/p/11564818.html