The first PHP extension that is useful

Source: Internet
Author: User
: This article mainly introduces the first PHP extension that has a certain effect. if you are interested in the PHP Tutorial, please refer to it. C/C ++ to develop PHP extensions

I think for PHP developers, there are a lot of things to learn. of course, developing PHP extensions is also a skill that must be mastered. here, I would like to worship Laruence) ~
The first feature extension to be developed today is to generate the date section before the log when recording logs.

# Even if the current time, year-month-day hour: minute: Second timestamp. subtle number [20:39:32 1432039172.968199]
The php Extension function starts by generating the "skeleton":
1. go to the src/ext directory of php source code 2. run. /ext_skel -- extname = mytest3. enter src/ext/mytest/directory 4. when writing extensions, you must first modify the config. 10-12 lines of the me file. remove the dnl comment.
Declare the php extension function name in the header file php_mttest.h.
PHP_FUNCTION (get_log_title); # PHP_FUNCTION is a macro declared in the PHP kernel # define PHP_FUNCTION ZEND_FUNCTION // in the file src/main/php. h: row 347 # define ZEND_FUNCTION (name) ZEND_NAMED_FUNCTION (ZEND_FN (name) // File src/Zend/zend_API.h: 68 rows # define ZEND_FN (name) zif _ # name // File src/Zend/zend_API.h: 65 rows # define ZEND_NAMED_FUNCTION (name) void name (INTERNAL_FUNCTION_PARAMETERS) // File src/Zend/zend_API.h: row 67 # define INTERNAL_FUNCTION_PARAMETERS int ht, zval * return_value, zval ** return_value_ptr, zval * this_ptr, int return_value_used TSRMLS_DC // src/Zend/zend. h: 290 rows

After the above declaration function macro is expanded:

void zif_get_log_title( int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_value_used TSRMLS_DC)
Add the get_log_title function declaration to the zend_function_entry declaration in mytest. c.
Const zend_function_entry logs_functions [] = {PHP_FE (confirm_logs_compiled, NULL)/* For testing, remove later. */PHP_FE (get_log_title, NULL) PHP_FE_END/* Must be the last line in logs_functions [] */}; # compile the function PHP_FUNCTION (get_log_title) {struct timeval TV; time_t rawtime; struct tm * timeinfo; char * formatinfo; time (& rawtime); timeinfo = localtime (& rawtime); gettimeofday (& TV, NULL); spprintf (& formatinfo, 0, "[% 4d-% 02d-% 02d % d: % d. % d] ", 1900 + timeinfo-> tm_year, 1 + timeinfo-> tm_mon, timeinfo-> tm_mday, timeinfo-> tm_hour, timeinfo-> tm_min, timeinfo-> tm_sec, TV. TV _sec, TV. TV _usec); RETURN_STRING (formatinfo, 1 );}

Here is a macro RETURN_STRING.

# Define RETURN_STRING (s, duplicate) {RETVAL_STRING (s, duplicate); return ;}// file src/Zend/zend_API.h: row 635 # define RETVAL_STRING (s, duplicate) ZVAL_STRING (return_value, s, duplicate) // File src/Zend/zend_API.h: row 623 # define ZVAL_STRING (z, s, duplicate) do {/// file src/Zend/zend_API.h: 577-583. the function is to assign a const char * _ s = (s) to a string type zval variable ); \ zval * _ z = (z); \ Z_STRLEN_P (_ z) = strlen (_ s); \\// set zval string length Z_ST RVAL_P (_ z) = (duplicate? Estrndup (_ s, Z_STRLEN_P (_ z) :( char *) _ s); \ // Set the zval string value Z_TYPE_P (_ z) = IS_STRING; \\// set the zval string type to string} while (0)

Therefore, the expanded RETURN_STRING (formatinfo, 1) is:

Do {const char * _ s = (formatinfo); zval * _ z = (return_value); // The return_value is estimated to be defined by the kernel, similar to zval * return_value; (* _ z ). value. str. len = strlrn (_ s); // equivalent to _ z-> value. str. len (* _ z ). value. str. val = (1? Estrndup (_ s, (* _ z). value. str. len) :( char *) _ s); (* _ z). type = 6 ;}
Compilation test
Php needs to be installed during compilation, for example, cd mytest is installed under the/usr/local/php/directory; // The function extension directory/usr/local/php/bin/phpize/run phpize to generate the configure file. /configure -- with-php-config =/usr/local/php/bin/php-config // configure, the following -- with-php-config will generate the so file to the installed extension directory. of course, you do not need to add it, if you manually copy the previous make // errors, correct make install // after running the command. the prompt is similar to the following # Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/# Modify the configuration file php. add extension = mytest. so # test php-r 'Var _ dump (log_get_title ());'

If you output the following content, congratulations!

[Root @ iforever logs] # php-r 'Var _ dump (get_log_title (); '& datestring (39) "[May 19, 2015 22:52:29 1432047149.755613]" 22:52:29 CST

5/19/2015 10:54:38

The copyright of this article is owned by the author iforever (luluyrt@163.com), without the author's consent to prohibit any form of Reprint, repost the article must be in the obvious position on the article page to give the author and the original connection, otherwise, you are entitled to pursue legal liability.

The above introduces the first PHP extension that has a certain effect, including some content, and hopes to be helpful to friends who are interested in PHP tutorials.

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.