Write your own PHP extension to create a common variable

Source: Internet
Author: User
Tags php write
Statement: This article is an original article by the author, all of which have been analyzed by the author one by one. I hope you can give me some advice if there is something wrong with it. Imsiren. comarchives568PHP write extension .. if you have read the original: We recommend that you read this article first when using CC ++ to expand PHP .. A simple one today .. we create a variable $ siren in extension mode. then, enter the PHP File

Statement: This article is an original article by the author, all of which have been analyzed by the author one by one. I hope you can give me some advice if there is something wrong with it. Http://imsiren.com/archives/568 PHP write extension .. if you have read the original: use C/C ++ to expand PHP, we recommend that you read this article first .. A simple one today .. we create a variable $ siren in extension mode. then, enter the PHP File

Statement: This article is an original article by the author, all of which have been analyzed by the author one by one. I hope you can give me some advice if there is something wrong with it.

Http://imsiren.com/archives/568

PHP write extension ..

If you still have a read Source: use C/C ++ to expand PHP

We recommend that you read this article first ..

Let's make a simple one today. We use the extension method to create a variable $ siren.

Then output the variable in the PHP file.

1. Execute ext_skel-extname = siren in the source code directory.

After the execution is successful, a siren file is generated in the php-src/ext/directory, which contains the basic structure of the extension.

2. Modify config. m4

Because my system is Linux, we need to modify this file. We use the so module to load the file, so edit the config. m4 file.

dnl PHP_ARG_WITH(siren, for siren support,dnl Make sure that the comment is aligned:dnl [  --with-siren             Include siren support])
Http://imsiren.com/archives/547

3. Edit php_siren.h.

This file is a header file that contains the definition of our function.

To create a variable function, first add a line of code in the file: PHP_FUNCTION (variable); it can be understood as declaring a function.

4. Modify the siren. c source file

Add a line of PHP_FE (variable, NULL) in the siren_functions array );

Then add the following code at the bottom

PHP_FUNCTION(variable){        zval* val;        MAKE_STD_ZVAL(val);        ZVAL_STRING(val,"this is siren",1);        ZEND_SET_SYMBOL(EG(active_symbol_table),"siren",val);}

The zval interface is used to save variable information.
The MAKE_STD_ZVAL macro is used to allocate memory space.
ZVAL_STRING macro sets the zval structure.
const char *__s=(s);                    \Z_STRLEN_P(z) = strlen(__s);    \Z_STRVAL_P(z) = (duplicate?estrndup(__s, Z_STRLEN_P(z)):(char*)__s);\Z_TYPE_P(z) = IS_STRING;

If you do not understand, see Original: PHP kernel research: HASH tables and variables
Finally, run the command in the php-src/ext/siren directory.
1. php installation directory/bin/phpize
2../configure-with-php-config =/php installation directory/bin/php-config
3. make & make install
After this execution, a siren. so file is generated under the extension directory of php.
4. Modify php. ini extension = siren. so to enable the extension.
5. Write the PHP file and execute variable (); Then echo $ siren;
Check whether "this is siren" is output?
Is it easy ..

Http://imsiren.com/archives/568

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.