PHP編寫擴展

開發環境:mac OS X EI Captionphp

IDE:Ecplise c/c++插件c++

php版本:5.5.36(當前的php版本>=開發版本)api

進入到ext擴展目錄下服務器

執行ext_skel腳本,能夠看到提示信息函數

--extname=module module is the name of your extension插件

在次執行./ext_skel --extname=test指針

能夠看到目錄下新增了一個test文件夾code

進入test文件夾下,修改config.m4配置文件開發

dnl PHP_ARG_WITH(test, for test support,
dnl Make sure that the comment is aligned:
dnl [  --with-test             Include test support])

//dnl表示註釋須要取消掉

PHP_ARG_WITH(test, for test support,
[  --with-test             Include test support])

執行phpize,能夠看到多出了configure文件,能夠幫助咱們檢測頭文件,環境字符串

執行configure文件,此處應該指向你的php-config目錄

./configure --with-php-config=/Applications/XAMPP/xamppfiles/bin/php-config

執行sudo make install 安裝擴展

執行php -i | grep php.ini 找到php.ini文件

php.ini文件上加上test.so

重啓服務器

執行php -m 看到test擴展就成功了

php_test.h頭中聲明一個函數PHP_FUNCTION(test);

test.c中實現這個函數

PHP_FUNCTION(test){
	/*定義一個int 型變量*/
	long a;
	long b;
	char *c;
	/* 字符串在c中使用指針,而且須要指定長度*/
	int c_len;
	/* 此處字符串一個s 對應兩個參數*/
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lls", &a, &b,&c,&c_len) == FAILURE) {
			return;
		}
	char * str;
	int len = spprintf(&str, 0, "%s:%d\n",c,a*b);
	/*能夠在zend_api下查看*/
	RETURN_STRINGL(str, len, 0);
}

注意,須要將test函數註冊到zend_api中,不然會報not found

const zend_function_entry test_functions[] = {
/*擴展定義的函數*/
	PHP_FE(test,NULL)
	PHP_FE_END	/* Must be the last line in test_functions[] */
};

從新

sudo make && make install

在php 文件中寫下echo test(100,200,"data");

得出 data=20000既成功

相關文章
相關標籤/搜索