In Linux, php5.3.2 calls so

Source: Internet
Author: User

The common method for PHP to call so is to use the DL () function, but this method is no longer supported after php5.3. It only supports static calls. for methods to use DL () for dynamic calls, see:

Reference page 1: http://tech.idv2.com/2007/07/06/use-local-so-in-php/

Thanks to the clear and clear methods provided by the original author, but I did not pass in version 5.3.1 I, so I found the page:

Reference page 2: http://www.9php.com/FAQ/cxsjl/php/2007/11/9405479103083.html

This page describes how to call functions in the so file statically. The solution is as follows:

 

Environment: Apache 2.2.9

Php5.3.2

 

First, use the method provided in reference page 1 to generate the dynamic library libhello. So.

 

Enter the PHP installation package decompression directory

 

-Laptop :~ /Dev/software/php-5.3.2 $ CD ext

Funny @ funny-LAPTOP :~ /Dev/software/php-5.3.2/EXT $

 

Create intermediate PHP link so project cltest

Funny @ funny-LAPTOP :~ /Dev/software/php-5.3.2/EXT $./ext_skel -- extname = cltest

Creating directory cltest

Creating basic files: config. M4 config. w32. cvsignore cltest. c php_cltest.h credits experimental tests/001. phpt cltest. php [done].

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

1. $ CD ..

2. $ vi ext/cltest/config. M4

3. $./buildconf

4. $./configure -- [with | enable]-cltest

5. $ make

6. $./PHP-f ext/cltest. php

7. $ vi ext/cltest. c

8. $ make

Repeat steps 3-6 until you are satisfied with ext/cltest/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.

Modify config. M4

Funny @ funny-LAPTOP :~ /Dev/software/php-5.3.2/EXT $ VI cltest/config. M4

Delete 3 DNL

DNL php_arg_with (my_module, for my_module support,

DNL make sure that the comment is aligned:

DNL [-- with-my_module include my_module support])

Or delete the three DNL

DNL php_arg_enable (my_module, whether to enable my_module support,

DNL make sure that the comment is aligned:

DNL [-enable-my_module enable my_module support])

Modification completed.

 

Add the PHP function definition to the main program file of the cltest. c extension module.

The main program describes the declaration of the PHP extension module, the number of functions contained in the module, the role of each function, the content displayed in the phpinfo function, and the initialization of the module, this file will describe what you want to end. We just added a function say_hello and described the specific content of the say_hello function.

[Root @ test1 ext] # vi cltest. c

Function_entry my_module_functions [] = {

Php_fe (say_hello, null )/*? Add this line of code to register the function say_hello ()*/

Php_fe (hello_add, null )/*? Add this line of code to register the function hello_add ()*/

Php_fe (confirm_my_module_compiled, null)/* for testing, remove later .*/

{Null, null, null}/* must be the last line in my_module_functions [] */

};

Add the following code function program subject at the end of the file

Php_function (say_hello)

{

Return_stringl ("Hello World", 11,1 );

}

 

Php_function (hello_add)

{

Long int A, B;

Long int result;

If (zend_parse_parameters (zend_num_args () tsrmls_cc, "ll", & A, & B) = failure ){

Return;

}

Result = hello_add (A, B );

Return_long (result );

}

Modification completed.

 

Add the function to be registered when PHP is started in the header file of the cltest. h extension module

In the corresponding header file, declare the say_hello function to complete our expected functions.

Funny @ funny-LAPTOP :~ /Dev/software/php-5.3.2/EXT $ gedit cltest/php_cltest.h

In the file php_function (confirm_my_module_compiled); Add the following code function definition before a line

Php_function (say_hello );

Php_function (hello_add );

 

Save the file and exit

 

Then run the phpize program to generate the configure script:

$ Phpize

Funny @ funny-LAPTOP :~ /Dev/software/php-5.3.2/EXT $ CD cltest

Funny @ funny-LAPTOP :~ /Dev/software/php-5.3.2/EXT/cltest $ phpize

Processing ing:

Php api version: 20090626

Zend module api no: 20090626

Zend extension api no: 220090626

 

Compilation can be completed after preparation.

$./Configure

Because you want to call the libhello. So dynamic library generated in reference page 1, use the following method edge

$ Make ldflags =-lhello

 

After compilation, cltest is generated under modules. so file: This file meets PHP call requirements. We only need to put this file under the directory of the module to be loaded by PHP and write the module name to PhP. INI: restart the server.

 

The procedure is as follows:

 

Add path in PHP. ini
Extension_dir =/opt/PHP/lib/PHP/extensions

Extension = gettext. So

Save and restart Apache

 

Test

 

<? PHP
Echo hello_add (3, 4 );
?>

Output 7

Or

 

<? PHP
Echo say_hello ();
?>

Output Hello World

 

 

Function call successful

 

 

 

 

 

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.