Home > Backend Development > PHP Tutorial > Getting started with writing PHP extensions

Getting started with writing PHP extensions

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:15:20
Original
930 people have browsed it

This article explains how PHP extensions are written by writing a simple PHP extension hello_world. This extension has no practicality and is purely used to learn how to write extensions. If you really want to write a practical PHP extension yourself, you need to be familiar with the ZEND API and have high requirements for C language.

Okay, let’s get to the point.

1. Enter the ext directory of the PHP source code, and then execute:

<code><span>./ext_skel --extname</span>=<span>hello_world</span></code>
Copy after login

2. Enter the hello_world directory, edit config.m4, and remove the dnl in front of lines 16, 18 and 53:

<code><span>PHP_ARG_ENABLE</span>(<span>hello_world</span>, <span>whether</span><span>to</span><span>enable</span><span>hello_world</span><span>support</span>,
<span>[  --enable-hello_world           Enable hello_world support]</span>)
<span>AC_DEFINE</span>(<span>HAVE_HELLO_WORLDLIB</span>,1,<span>[ ]</span>)
</code>
Copy after login

3. Modify php_hello_world. h file, change the original

<code><span>#<span>define</span> PHP_HELLO_WORLD_H</span></code>
Copy after login

to the following content:

<code><span>#<span>define</span> PHP_HELLO_WORLD_H 1</span><span>#<span>define</span> PHP_HELLO_WORLD_VERSION "1.0"</span><span>#<span>define</span> PHP_HELLO_WORLD_EXTNAME "hello_world"</span>
PHP_FUNCTION(hello_world);<span>//这句最关键</span></code>
Copy after login

4. Modify the hello_world.c file:
Change the following content:

<code><span>const</span> zend_function_entry hello_world_functions[] = {
        PHP_FE(confirm_hello_world_compiled,    <span>NULL</span>)           <span>/* For testing, remove later. */</span>
        PHP_FE_END      <span>/* Must be the last line in hello_world_functions[] */</span>
};</code>
Copy after login

to:

<code><span>const</span> zend_function_entry hello_world_functions[] = {
        PHP_FE(confirm_hello_world_compiled,    <span>NULL</span>)           <span>/* For testing, remove later. */</span>        PHP_FE(hello_world,    <span>NULL</span>)
        PHP_FE_END      <span>/* Must be the last line in hello_world_functions[] */</span>
};
</code>
Copy after login

Then add the following content at the end:

<code><span>//扩展函数正文部分</span><span>PHP_FUNCTION(hello_world)</span>{
        <span>RETURN_STRING(<span>"Hello World!"</span>,<span>1</span>)</span>;
}</code>
Copy after login

5. Go to the ext/hello_world directory and execute the following command

<code>/usr/<span>local</span>/php/bin/phpize
<span>.</span>/configure <span>--</span><span>with</span><span>-php</span><span>-config</span><span>=</span>/usr/<span>local</span>/php/bin/php<span>-config</span>
make <span>&&</span> make install
</code>
Copy after login

6. Edit php.ini and add the following content:

<code><span>extension</span>=<span>hello_world.so</span></code>
Copy after login

Test:
Execute the following content on the command line:

<code>php <span>-r</span><span>"echo hello_world();"</span></code>
Copy after login

At this time, you will see the following content on the standard output:

<code><span>Hello</span><span>World</span><span>!</span></code>
Copy after login

At this point, the hello_world extension is written!

Copyright Statement: This article is an original article by the blogger. Please indicate the source when reprinting.

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

Related labels:
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
Latest Issues
Hello everyone, new report
From 1970-01-01 08:00:00
0
0
0
When myhomepage opens, it prompts Hello World
From 1970-01-01 08:00:00
0
0
0
Hello, export your data table there
From 1970-01-01 08:00:00
0
0
0
Hello, what is the group number?
From 1970-01-01 08:00:00
0
0
0
Can only access index, not hello
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template