Use phpize to create PHP extensions

Source: Internet
Author: User

Today I want to talk about the php extension in linux.

Next, let's talk about how to make a php extension.
First, you must have a set up php environment.
I installed php in/usr/local/php. Of course
A php path for configuring php. ini, but pay attention to it.
Php extensions installed in this way cannot be implemented
The/usr/local/php/bin directory after php installation
Find the file phpize. We will use it later.
He is a shell script. You can use vi phpize to view his content.
But you should note that this script can not be applied anywhere.
[Root @ ns root] # phpize
Cannot find config. m4.
Make sure that you run '/usr/local/bin/phpize' in the top level source directory of the module

[Root @ ns root] # phpize
Cannot find config. m4.
Make sure that you run '/usr/local/bin/phpize' in the top level source directory of the module

You will see these two results. You actually checked the script.
You can easily find out how to handle it.
It's best for you to scale your model.
Put under/usr/local/src/php-4.3.5/ext
You can also run it here/usr/local/php/bin/phpize.
Phpize to execute

We found this tool under/usr/local/src/php-4.3.5/ext
To build a php extension framework.
[Root @ ns ext] # cd/usr/local/src/php-4.3.5/ext/
[Root @ ns ext] #./ext_skel -- extname = jinzhesheng_module
Creating directory jinzhesheng_module
Creating basic files: config. m4. cvsignore jinzhesheng_module.c php_jinzhesheng_module.h credits experimental tests/001. phpt jinzhesheng_module.php [done].

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

1. $ cd ..
2. $ vi ext/jinzhesheng_module/config. m4
3. $./buildconf
4. $./configure -- [with | enable]-jinzhesheng_module
5. $ make
6. $./php-f ext/jinzhesheng_module/jinzhesheng_module.php
7. $ vi ext/jinzhesheng_module/jinzhesheng_module.c
8. $ make
After performing this step, you will see this result.
Repeat steps 3-6 until you are satisfied with ext/jinzhesheng_module/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.
In this case, a directory named jinzhesheng_module will be generated under this directory.
Let's take a look.
[Root @ ns ext] # cd jinzhesheng_module/
[Root @ ns jinzhesheng_module] # ls
Config. m4 EXPERIMENTAL jinzhesheng_module.php tests
CREDITS jinzhesheng_module.c php_jinzhesheng_module.h

Then we want to modify the file order as follows:
Configue. m4
Jinzhesheng_module.c
Php_jinzhesheng_module.h

Open config in the text editor. m4 file. The file content is roughly as follows: dnl $ Id $ ddnl config. m4 for extension my_modulednl don't forget to call PHP_EXTENSION (my_module) dnl Comments in this file start with the string 'dnl '. dnl Remove where necessary. this file will not workdnl without editing. dnl If your extension references something external, use:

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])

Dnl Otherwise use enable:

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])

If test "$ PHP_MY_MODULE "! = "No"; then

Dnl If you will not be testing anything external, like existence

Dnl headers, libraries or functions in them, just uncomment

Dnl following line and you are ready to go. dnl Write more examples of tests here...

PHP_EXTENSION (my_module, $ ext_shared)

Fi will

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])

Modify

PHP_ARG_WITH (my_module, for my_module support,

Make sure that the comment is aligned:

[-- With-my_module Include my_module support])

Or set

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])

Modify

PHP_ARG_ENABLE (my_module, whether to enable my_module support,

Make sure that the comment is aligned:

[-Enable-my_module Enable my_module support])
I used the latter here
Save and exit
Then Edit
Vi my_module.c
Modify the following code in the file.

/* Every user visible function must have an entry in my_module_functions [].

*/Function_entry my_module_functions [] = {

PHP_FE (say_hello, NULL )/*? Add a line of code */

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 PHP_FUNCTION (say_hello) at the end of the file ){

Zend_printf ("hello world/n ");

} Save the file and exit

Then we can use the above command in this directory.
/Usr/local/php/bin/phpize
After execution, you will see the following
[Root @ ns jinzhesheng_module] #/usr/local/php/bin/phpize
Processing ing:
PHP Api Version: 20020918
Zend Module Api No: 20020429
Zend Extension Api No: 20050606
[Root @ ns jinzhesheng_module] #
Then execute. /configure -- enable-jinzhesheng_module -- with-apxs =/usr/local/apache/bin/apxs -- with-php-config =/usr/local/php/bin/php-config
We can find this file in the bin directory of php after installation
Php-config and phpize
Note where your apache apxs is stored in this step.
Then execute make
You will see an error. You have redefined the function.
You can change the file and delete the original function. The generated file contains the same function.
You are adding your code
You can pass
At this time, a directory named modules will be generated under the current directory, and the contents you want will be placed below it.
Jinzhesheng_module.so File
Cp modules/jinzhesheng_module.so/usr/local/php/ext/
You need to set the extension directory of your php In
In php. ini
Use extension_dir
The last step is not to open this extension in the php. ini file.
Extension = jinzhesheng_module.so
Then
Restart apache
Use phpinfo to check whether it is OK.

Address: http://info.codepub.com/2008/08/info-21342.html

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.