When a feature is compiled into the so file, how do you invoke it through PHP? One way is to write a PHP module (PHP extension), call the function in PHP, and then use the module to call the function in so. Here's a simple example of using the OS Fedora Core 6.
First, make a simple so file:
/**
* HELLO.C
* To compile, use following commands:
* Gcc-o-c-fpic-o hello.o hello.c
* Gcc-shared-o libhello.so hello.o
*/
int Hello_add (int a, int b)
{
return a + B;
}
It is then compiled into a. So file and placed in the system:
$ gcc-o-c-fpic-o hello.o hello.c
$ gcc-shared-o libhello.so hello.o
$ su
# Echo/usr/local/lib >/etc/ld.so.conf.d/local.conf
# CP Libhello.so/usr/local/lib
#/sbin/ldconfig Write a small program to verify its correctness:
/**
* HELLOTEST.C
* To compile, use following commands:
* Gcc-o Hellotest-lhello hellotest.c
*/
#include <stdio.h>
int main ()
{
int a = 3, b = 4;
printf ("%d +%d =%d\n", A, B, Hello_add (a,b));
return 0;
}
Compile and execute:
$ gcc-o Hellotest-lhello hellotest.c
$./hellotest
3 + 4 = 7OK,
Here we will make the PHP module. First make sure you install the Php-devel package, if not, please find it by yourself from the installation CD. Then download the PHP source code. I'm using php-5.2.9.tar.gz, decompression.
$ tar xzvf php-5.2.9.tar.gz
$ CD Php-5.2.9/ext then create a module named Test with the following command.
$./ext_skel--extname=test After executing the command, it prompts you what commands should be used to compile the module, but it is a compilation method that integrates the module into the PHP interior. The method is simpler if you want to compile into a php_test.so that can be dynamically loaded.
$ cd Test First edit the Config.m4 file and remove the comments from lines 16th and 18th (the annotation symbol is DNL.) )
16:php_arg_enable (test, whether to ENABLE test support,
17:DNL Make sure this comment is aligned:
[--enable-test Enable test support]) then execute the phpize () program to generate the Configure script:
$ phpize then opens Php_test.h, in Php_function (confirm_test_compiled); Add function declaration below:
Php_function (confirm_hello_compiled); /* For testing, remove later. */
Php_function (Test_add); open hello.c, add the following below Php_fe (confirm_test_compiled, NULL).
Zend_function_entry test_functions[] = {
Php_fe (confirm_test_compiled, NULL)/* For testing, remove later. */
Php_fe (Test_add, NULL)/* For testing, remove later. */
{null, NULL, NULL}/* must is the last line in test_functions[] * *
};
Then write the contents of the Test_add function at the very end of the test.c:
Php_function (Test_add)
{
Long int A, b;
long int result;
if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "ll", &a, &b) = = failure) {
Return
}
result = Test_add (A, b);
Return_long (result);
}
Save exit, compile and install:
$./configure--enable-jinzhesheng_module--with-apxs=/usr/local/apache/bin/apxs--with-php-config=/usr/local/php/ bin/php-config$ make
# CP Modules/test.so/usr/lib/php/modules Then create a test.php file under/www/web/, which reads as follows:
<?php
DL ("test.so");
Echo Test_add (1, 2);
?>
The test.php file is then opened in the browser, and if 3 is displayed, the function call succeeds.