Developing PHP extensions using C++

WBOY
Release: 2016-08-08 09:21:42
Original
1435 people have browsed it
Reprinted from: http://521-wf.com/archives/241.htmlThis article mainly introduces the differences between using C++ to develop PHP extensions and C languageAt present, the PHP programming language is also quite mature, and various All kinds of documents, all kinds of questions, just Google, there will always be the answers you want. Of course, there are many articles on "How to develop PHP extensions", but there are few that specifically introduce the use of C++ to develop PHP extensions. The powerful functions of the C++ programming language have prompted many companies to choose to use it for backend programs. Therefore, most situations we encounter require us to use C++ to extend PHP. The extended skeleton tool in the PHP source code is generated by default to support C language. If you want to use C++ for development, some parameters need to be configured separately. A simple example will be used to illustrate below. Preparation: 1. Download the PHP source code http://www.php.net/downloads, what is downloaded here is php-5.3.24.tar.gz
2. After decompression, the source code is placed in / root/php-5.3.24/
3. Installation directory: /usr/local/php-5.3.24/
4. Start the installation and configure the path of php.ini to facilitate future personalized configuration
5. Extension name: discuz
6. Extension function: discuz_say(), this function only returns a "Hello world!" string
7. The extension can run on win32 systems and also on unix-like systems, but different files need to be compiled, which are only introduced here. Operation under GNU/Linux. Start writing the extension: 1. Create the function list file discuz.proto to be implemented, the content is as follows: string discuz_say()2. Use the extension skeleton tool to generate the core file, the command is as follows: [root@localhost ~]# cd php-5.3.24/ext/ [root@localhost ext]# ./ext_skel --extname=discuz --proto=../../discuz.protoAt this time in ext A discuz folder appears in the directory, which contains several files, such as: config.m4 discuz.c php_discuz.h and so on. 3. Modify the config.m4 file, the content is as follows: dnl $Id$ dnl config.m4 for extension discuz PHP_ARG_ENABLE(discuz, whether to enable discuz support, Make sure that the comment is aligned: [ --enable-discuz Enable discuz support]) if test "$PHP_DISCUZ" != "no"; then PHP_REQUIRE_CXX() dnl 通知Make使用g++ PHP_ADD_LIBRARY(stdc++, 1, EXTRA_LDFLAGS) dnl 加入C++标准库 PHP_NEW_EXTENSION(discuz, discuz.cpp, $ext_shared) fiIn this file, dnl is the comment character, and the string after it is the interpretation context. 4. Modify the discuz.c file and rename it to discuz.cpp (this name looks more professional) 4.1 Add the required C++ string header file, as follows: #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_discuz.h" #include /* 添加这行 */4.2 Modify the discuz_say function, as follows: /* {{{ proto string discuz_say() */ PHP_FUNCTION(discuz_say) { std::string str = "Hello world!"; RETURN_STRINGL(str.c_str(), str.length(), 1); } 5. Compile the extension and put its discuz.so in the installation directory (you can refer to Two ways to compile PHP extensions). Start testing: [root@localhost ~]# /usr/local/php-5.3.24/bin/php hi.php Hello world!At this point, a simple extension is completed

The above introduces the use of C++ to develop PHP extensions, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!