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.
You are welcome to reprint it. Please indicate the source for reprinting.
Address: 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])

Remove 1, 3 lines of annotation DNL, so that we can use PHP to load in the form of a module .. For details, see my previous article 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 ..

Source: 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.