Home >Backend Development >PHP Tutorial >PHP extension, an implementation of helloworld_PHP tutorial

PHP extension, an implementation of helloworld_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:56:14952browse

php extension, an implementation of helloworld



php -v
PHP 5.5.9-1ubuntu4.7 (cli) (built: Mar 16 2015 20:47:39)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans

Configuration reference article: http://blog.csdn.net/niujiaming0819/article/details/8543028
This article also uses the steps in the reference article to implement the process on this machine

step 1. Download the php source code package. You can download it from the php official website. This time, the php5.6.8 version is used
step 2. Unzip the compressed package and enter the decompressed package (mysoft$ cd php-5.6.8/). Use the ls command to view the file directory in the source package and enter the ext folder (cd ext)

PHP extension, an implementation of helloworld_PHP tutorial

step 3. Establish an extension development framework ./ext_skel --extname=helloworld

Execute the above command to establish the framework for expansion development. You will find that there is an additional folder called helloworld under the ext folder, and some text is also output from the command line,

You will be prompted with the general steps to generate the extension.

PHP extension, an implementation of helloworld_PHP tutorial


PHP extension, an implementation of helloworld_PHP tutorial



step 4. Enter the root directory of the php source code and edit the file vim ext/helloworld/config.m4

Remove a few lines of comments in this file, which will be roughly between lines 16-19. The specific version may be different and then save the file (: wq)

PHP extension, an implementation of helloworld_PHP tutorial

step 5. Execute the command ./buildconf --force

in the php source code root directory

PHP extension, an implementation of helloworld_PHP tutorial

step 6. Compile the php program in the root directory of the php source code. Note that the command is ./configure --with-helloworld

PHP extension, an implementation of helloworld_PHP tutorial

The last error that occurred was not processed

PHP extension, an implementation of helloworld_PHP tutorial

step 7. Enter our extension directory helloworld and execute the command phpize (install phpize through sudo apt-get install php5-dev). At this time, your extension directory will generate many files that can be used Post compilation.

PHP extension, an implementation of helloworld_PHP tutorial

step 8.Compile our extension in the helloworld directory./configure --with-php-config=/usr/bin/php-config (use the php-config of your own environment) -- enable-helloworld

You can find the location of your php-config file through a command (find / -name php-config) as shown in the figure: My address is /usr/bin/php-config

PHP extension, an implementation of helloworld_PHP tutorial

PHP extension, an implementation of helloworld_PHP tutorial

step 9. Enter the extended helloworld directory, edit the file php_helloworld.h, and add the function PHP_FUNCTION(helloworldTest);

in the last line

helloworldTest can be changed to your favorite name, then save and exit

PHP extension, an implementation of helloworld_PHP tutorial

step 10. Open helloword.c with vim, implement our function in helloworld.c, then add the helloworldTest function to helloworld_functions[], save and exit

PHP extension, an implementation of helloworld_PHP tutorial

PHP extension, an implementation of helloworld_PHP tutorial

step 11.Execute the make command to compile the extension. I ran it relatively smoothly. If an error occurs, please carefully check the previous steps to see if there are any errors. I also made errors when I did it for the first time. Generally, there is a problem with the previous steps.

PHP extension, an implementation of helloworld_PHP tutorial

step12. Copy the compiled helloworld.so file to your local php extension directory

The extension directory can be viewed through php -r phpinfo(); | grep extension_dir to view the extension path of native php

PHP extension, an implementation of helloworld_PHP tutorial

step 13. Configure php.ini to enable helloworld.so extension

View the location of php.ini (php -r phpinfo(); | grep php.ini )

PHP extension, an implementation of helloworld_PHP tutorial

vim opens the php.ini file sudo vim /etc/php5/cli/php.ini

Add extension (extension=helloworld.so) at the end of the file

Tested by php -r phpinfo(); | grep helloworld

PHP extension, an implementation of helloworld_PHP tutorial

step 14. Test extension (php -r echo helloworldTest();)

PHP extension, an implementation of helloworld_PHP tutorial

During this process, I tried following the original author’s steps, but an error occurred

Segmentation fault” (core dumped)

After debugging, the original author changed PHP_FE_END to {null,null,null} when adding it to helloworld_functions[] in the helloworld.c file. When he compiled it again, there was no error, but he also commented PHP_FE_END. Modification After that, just recompile and copy.





www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/989127.htmlTechArticlephp extension, a helloworld implementation php -v PHP 5.5.9-1ubuntu4.7 (cli) (built: Mar 16 2015 20:47:39) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn