Table of Contents
C/C++ to develop PHP extensions
Prelude to the php extension function
First generate the "skeleton":
In the header file Declare the name of the PHP extension function to be written in php_mttest.h
Add the declaration of the get_log_title function to the zend_function_entry declaration in mytest.c
Compile and test
Home Backend Development PHP Tutorial The first useful PHP extension

The first useful PHP extension

Aug 08, 2016 am 09:24 AM
function php return zend

C/C++ to develop PHP extensions

I feel that for PHP developers, there are very complex and many things to learn. Of course, developing PHP extensions is also a skill that must be mastered. Here I pay homage to the great bird brother ( Laruence)~
The first functional extension to be developed today. Its main function is to generate the date part in front of the log when recording the log

<code>#即使当前的时间,年-月-日 时:分:秒 时间戳.微妙数 
[2015-05-19 20:39:32 1432039172.968199]</code>
Copy after login

Prelude to the php extension function

First generate the "skeleton":
<code>1. 进入php源码src/ext目录下面
2. 运行./ext_skel --extname=mytest
3. 进入src/ext/mytest/目录
4. 在编写扩展的时候要先修改下config.me文件的10-12行,去掉前面的dnl注释,这个网上一搜一大把。</code>
Copy after login
In the header file Declare the name of the PHP extension function to be written in php_mttest.h
<code>PHP_FUNCTION(get_log_title);
#PHP_FUNCTION是PHP内核中声明的一个宏
#define PHP_FUNCTION 		ZEND_FUNCTION    //在文件src/main/php.h : 347行
#define ZEND_FUNCTION(name) ZEND_NAMED_FUNCTION(ZEND_FN(name))		//文件src/Zend/zend_API.h : 68行
#define ZEND_FN(name) 		zif_##name		//文件src/Zend/zend_API.h : 65行
#define ZEND_NAMED_FUNCTION(name) void name(INTERNAL_FUNCTION_PARAMETERS)	//文件src/Zend/zend_API.h : 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行</code>
Copy after login

The above declaration function macro is expanded to:

<code>void zif_get_log_title( int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_value_used TSRMLS_DC)</code>
Copy after login
Add the declaration of the get_log_title function to the zend_function_entry declaration in mytest.c
<code>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[] */
};
#编写函数
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 %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);
}</code>
Copy after login

There is also a macro to look at here RETURN_STRING

<code>#define RETURN_STRING(s, duplicate) 	{ RETVAL_STRING(s, duplicate); return; }		//文件src/Zend/zend_API.h : 635行
#define RETVAL_STRING(s, duplicate) 	ZVAL_STRING(return_value, s, duplicate)			//文件src/Zend/zend_API.h : 623行
#define ZVAL_STRING(z, s, duplicate) do {	\	//文件src/Zend/zend_API.h : 577-583行,这个的作用其实是给一个string类型zval变量赋值
	const char *__s=(s);				\		
	zval *__z = (z);					\
	Z_STRLEN_P(__z) = strlen(__s);		\		//设置zval字符串长度
	Z_STRVAL_P(__z) = (duplicate?estrndup(__s, Z_STRLEN_P(__z)):(char*)__s);\	//设置zval字符串的值
	Z_TYPE_P(__z) = IS_STRING;			\		//设置zval字符串的类型为字符串
} while (0)</code>
Copy after login

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

<code>do {
	const char *__s=(formatinfo);
	zval *__z = (return_value);			//这里面的return_value估计是内核定义的,类似 zval *return_value;
	(*__z).value.str.len = strlrn(__s);		//等价于__z->value.str.len
	(*__z).value.str.val = (1?estrndup(__s, (*__z).value.str.len):(char*)__s);
	(*__z).type = 6;	
}</code>
Copy after login
Compile and test
<code>编译的时候需要安装好的php,比如我的php安装在/usr/local/php/目录下面

cd mytest;		//今日刚才编写的函数扩展目录
/usr/local/php/bin/phpize 	//运行phpize,生成configure文件
./configure --with-php-config=/usr/local/php/bin/php-config 	//configure,后面的--with-php-config会生成so文件到安装好的扩展目录,当然也可以不用加,声称之后手动copy过去
make	//有错误的话修正
make install 	//运行之后会提示类似下面的内容
#Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
#修改配置文件php.ini添加extension = mytest.so
#测试
php -r 'var_dump(log_get_title());'</code>
Copy after login

If you output the following content, congratulations on your success

<code>[root@iforever logs]# php -r 'var_dump(get_log_title());' && date
string(39) "[2015-05-19 22:52:29 1432047149.755613]"
2015年 05月 19日 星期二 22:52:29 CST</code>
Copy after login

5/19/2015 10:54:38 PM

The copyright of this article belongs to the author iforever (luluyrt@163.com). Any form of reprinting is prohibited without the consent of the author. After reprinting the article, the author and the original text link must be provided in an obvious position on the article page, otherwise the right to pursue legal liability is reserved.

The above introduces the first useful PHP extension, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles