allowedExts php,通过php扩展增加一个内置函数
在日常的調試中,我們經常需要輸出數組,但是print_r函數輸出的數組是沒有格式的,不太好看,
為了方便,我們只要自己增加一個函數,如
function dump($arr) {
echo '< pre>';
print_r($arr);
echo '< /pre>';
}
例如thinkphp就是這么做的,但是個人感覺太麻煩了,
如果php有個類似的內置函數,那不是方便多了?為此我們可以更改print_r的源代碼,讓他在輸出數組前先輸出pre標簽,print_r的源代碼在ext/standard/basic_functions.c中,
但是呢,修改php的原有函數畢竟不太好,我們還是新添加一個擴展好了。
我們添加一個tiyee擴展,增加一個dump()函數
代碼如下
cd /path/to/phpsource/ext
./ext_skel --extname=tiyee
cd tiyee
vi config.m4
出現如下提示
$ cd ..
$ vi ext/tiyee/config.m4
$ ./buildconf
$ ./configure --\[with|enable\]-tiyee
$ make
$ ./sapi/cli/php -f ext/tiyee/tiyee.php
$ vi ext/tiyee/tiyee.c
$ make
Repeat steps 3-6 until you are satisfied with ext/tiyee/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.
然后進入生成的文件夾,打開config.m4
cd tiyee
vi config.m4
然后,將里面的代碼里面的16-18行前面的dnl注釋去掉,即改成如下
PHP_ARG_ENABLE(tiyee, whether to enable tiyee support,
Make sure that the comment is aligned:
[ --enable-tiyee Enable tiyee support])
然后運行
phpize
生成如下提示
Configuring for:
PHP Api Version: 20121113
Zend Module Api No: 20121212
Zend Extension Api No: 220121212
打開php_tiyee.h,在PHP_FUNCTION(confirm_tiyee_compiled); /* For testing, remove later. */下面增加一行(你其實也可以直接更改它)
PHP_FUNCTION(confirm_tiyee_compiled); /* For testing, remove later. */
PHP_FUNCTION(dump);
然后打開tiyee.c,在PHP_FE(confirm_tiyee_compiled, NULL) /* For testing, remove later. */下面增加一行
改成
PHP_FE(confirm_tiyee_compiled, NULL) /* For testing, remove later. */
PHP_FE(dump, NULL)
然后下面增加具體的函數,可以在
PHP_FUNCTION(confirm_tiyee_compiled)
{
char *arg = NULL;
int arg_len, len;
char *strg;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "jumtao", arg);
RETURN_STRINGL(strg, len, 0);
}
增加一行,變成
PHP_FUNCTION(dump)
{
zval *var;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &var) == FAILURE) {
RETURN_FALSE;
}
printf("< pre >\n");
zend_print_zval_r(var, 0 TSRMLS\_CC);
printf("< /pre > \n");
}
PHP_FUNCTION(confirm_tiyee_compiled)
{
char *arg = NULL;
int arg_len, len;
char *strg;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "jumtao", arg);
RETURN_STRINGL(strg, len, 0);
}
現在我們可以編譯了
./configure --with-php-config=/usr/local/php/bin/php-config /*后面的php-config地址按你的實際地址寫*/
make
make install
然后再php.ini里添加
extension=tiyee.so
然后重啟php-fpm
試試,這時試試,是不是可以了!
總結
以上是生活随笔為你收集整理的allowedExts php,通过php扩展增加一个内置函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java做一个简单的数据库,哪个嵌入式数
- 下一篇: php 整数 比较,php中字符串和整数