亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺談PHP Extension的開(kāi)發(fā)——基礎(chǔ)篇第2/2頁(yè)

 更新時(shí)間:2010年12月09日 21:47:38   作者:  
要開(kāi)發(fā)PHP擴(kuò)展,第一步要下載PHP源代碼,因?yàn)槔锩嬗虚_(kāi)發(fā)擴(kuò)展需要的工具。我下載的是PHP最新版本5.3.3,格式為tar.bz2壓縮包。

下面是“say_hello.c”中需要編寫的info_func的具體代碼:

復(fù)制代碼 代碼如下:

/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(say_hello)
{
php_info_print_table_start();
php_info_print_table_header(2, "say_hello support", "enabled");
php_info_print_table_row(2, "author", "Zhang Yang"); /* Replace with your name */
php_info_print_table_end();

/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */

可以看到我們編寫了兩行內(nèi)容、組件是否可用以及作者信息。

編寫核心函數(shù)
編寫核心函數(shù),總共分為三步:1、使用宏P(guān)HP_FUNCTION定義函數(shù)體;2、使用宏ZEND_BEGIN_ARG_INFO和ZEND_END_ARG_INFO定義參數(shù)信息;3、使用宏P(guān)HP_FE將函數(shù)加入到say_hello_functions中。下面分步說(shuō)明。

使用宏P(guān)HP_FUNCTION定義函數(shù)體
復(fù)制代碼 代碼如下:

PHP_FUNCTION(say_hello_func)
{
char *name;
int name_len;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE)
{
return;
}
php_printf("Hello %s!", name);

RETURN_TRUE;
}

上文說(shuō)過(guò),編寫PHP擴(kuò)展時(shí)幾乎所有東西都不能裸寫,而是必須使用相應(yīng)的宏。從上面代碼可以清楚看到這一點(diǎn)??傮w來(lái)說(shuō),核心函數(shù)代碼一般由如下幾部分構(gòu)成:

定義函數(shù),這一步通過(guò)宏P(guān)HP_FUNCTION實(shí)現(xiàn),函數(shù)的外部名稱就是宏后面括號(hào)里面的名稱。

聲明并定義局部變量。

解析參數(shù),這一步通過(guò)zend_parse_parameters函數(shù)實(shí)現(xiàn),這個(gè)函數(shù)的作用是從函數(shù)用戶的輸入棧中讀取數(shù)據(jù),然后轉(zhuǎn)換成相應(yīng)的函數(shù)參數(shù)填入變量以供后面核心功能代碼使用。zend_parse_parameters的第一個(gè)參數(shù)是用戶傳入?yún)?shù)的個(gè)數(shù),可以由宏“ZEND_NUM_ARGS() TSRMLS_CC”生成;第二個(gè)參數(shù)是一個(gè)字符串,其中每個(gè)字母代表一個(gè)變量類型,我們只有一個(gè)字符串型變量,所以第二個(gè)參數(shù)是“s”;最后各個(gè)參數(shù)需要一些必要的局部變量指針用于存儲(chǔ)數(shù)據(jù),下表給出了不同變量類型的字母代表及其所需要的局部變量指針。

image

參數(shù)解析完成后就是核心功能代碼,我們這里只是輸出一行字符,php_printf是Zend版本的printf。

最后的返回值也是通過(guò)宏實(shí)現(xiàn)的。RETURN_TRUE宏是返回布爾值“true”。

使用宏ZEND_BEGIN_ARG_INFO和ZEND_END_ARG_INFO定義參數(shù)信息

參數(shù)信息是函數(shù)所必要部分,這里不做深究,直接給出相應(yīng)代碼:

復(fù)制代碼 代碼如下:

ZEND_BEGIN_ARG_INFO(arginfo_say_hello_func, 0)
ZEND_END_ARG_INFO()

如需了解具體信息請(qǐng)閱讀相關(guān)宏定義。

使用宏P(guān)HP_FE將函數(shù)加入到say_hello_functions中
最后,我們需要將剛才定義的函數(shù)和參數(shù)信息加入到say_hello_functions數(shù)組里,代碼如下:
復(fù)制代碼 代碼如下:

const zend_function_entry say_hello_functions[] = {
PHP_FE(say_hello_func, arginfo_say_hello_func)
{NULL, NULL, NULL}
};

這一步就是通過(guò)PHP_EF宏實(shí)現(xiàn),注意這個(gè)數(shù)組最后一行必須是{NULL, NULL, NULL} ,請(qǐng)不要?jiǎng)h除。

下面是編寫完成后的say_hello.c全部代碼:
復(fù)制代碼 代碼如下:

/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/

/* $Id: header 297205 2010-03-30 21:09:07Z johannes $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_say_hello.h"

/* If you declare any globals in php_say_hello.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(say_hello)
*/

/* True global resources - no need for thread safety here */
static int le_say_hello;

/* {{{ PHP_FUNCTION
*/
PHP_FUNCTION(say_hello_func)
{
char *name;
int name_len;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE)
{
return;
}
php_printf("Hello %s!", name);

RETURN_TRUE;
}

ZEND_BEGIN_ARG_INFO(arginfo_say_hello_func, 0)
ZEND_END_ARG_INFO()
/* }}} */

/* {{{ say_hello_functions[]
*
* Every user visible function must have an entry in say_hello_functions[].
*/
const zend_function_entry say_hello_functions[] = {
PHP_FE(say_hello_func, arginfo_say_hello_func)
{NULL, NULL, NULL} /* Must be the last line in say_hello_functions[] */
};
/* }}} */

/* {{{ say_hello_module_entry
*/
zend_module_entry say_hello_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"say_hello",
say_hello_functions,
NULL,
NULL,
NULL,
NULL,
PHP_MINFO(say_hello),
#if ZEND_MODULE_API_NO >= 20010901
"0.1", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_SAY_HELLO
ZEND_GET_MODULE(say_hello)
#endif

/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(say_hello)
{
php_info_print_table_start();
php_info_print_table_header(2, "say_hello support", "enabled");
php_info_print_table_row(2, "author", "Zhang Yang"); /* Replace with your name */
php_info_print_table_end();

/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */

編譯并安裝擴(kuò)展
在say_hello目錄下輸入下面命令:
復(fù)制代碼 代碼如下:

/usr/bin/phpize
./configure
make
make install

這樣就完成了say_hello擴(kuò)展的安裝(如果沒(méi)有報(bào)錯(cuò)的話)。

這時(shí)如果你去放置php擴(kuò)展的目錄下,會(huì)發(fā)現(xiàn)多了一個(gè)say_hello.so的文件。如下圖所示:

image

下面就是將其加入到php.ini配置中,然后重啟Apache(如果需要的話)。這些都是PHP基本配置的內(nèi)容,我就不詳述了。

擴(kuò)展測(cè)試

如果上面順利完成,這時(shí)運(yùn)行phpinfo(),應(yīng)該能看到如下信息:

image

這說(shuō)明擴(kuò)展已經(jīng)安裝成功了。然后我們編寫一個(gè)測(cè)試用PHP腳本:

復(fù)制代碼 代碼如下:

<?php
say_hello_func('Zhang Yang');
?>

執(zhí)行這個(gè)腳本,結(jié)果如下:

image

說(shuō)明擴(kuò)展已經(jīng)正常工作了。

總結(jié)

這篇文章主要用示例方法介紹PHP Extension的開(kāi)發(fā)基礎(chǔ)。在PHP的使用中,也許是因?yàn)樾枰С中碌慕M件(如新的數(shù)據(jù)庫(kù)),又或是業(yè)務(wù)需要或性能需要,幾乎都會(huì)遇到需要開(kāi)發(fā)PHP擴(kuò)展的地方。后續(xù)如果有機(jī)會(huì),我會(huì)寫文章介紹一些關(guān)于擴(kuò)展開(kāi)發(fā)較為深入的東西,如擴(kuò)展模塊生命周期、INI使用以及編寫面向?qū)ο蟮臄U(kuò)展模塊等等。
本文基于署名-非商業(yè)性使用 3.0許可協(xié)議發(fā)布,歡迎轉(zhuǎn)載或演繹,但是必須保留本文的署名張洋(包含鏈接),且不得用于商業(yè)用途。如您有任何疑問(wèn)或者授權(quán)方面的協(xié)商,請(qǐng)與我聯(lián)系

相關(guān)文章

  • 推薦十款免費(fèi) WordPress 插件

    推薦十款免費(fèi) WordPress 插件

    本文給大家介紹的是今年必備的10款WordPress插件,非常的實(shí)用,包含WordPress SEO By Yoast,Akismet,W3 Total Cache,Disqus Comment System,Google Sitemap Generator,Jetpack,Limit Login Attempts,Contact Form 7,Yet Another Related Posts Plugin等
    2015-03-03
  • php flush無(wú)效,IIS7下php實(shí)時(shí)輸出的方法

    php flush無(wú)效,IIS7下php實(shí)時(shí)輸出的方法

    這篇文章主要介紹了php flush無(wú)效,IIS7下php實(shí)時(shí)輸出的方法,需要的朋友可以參考下
    2016-08-08
  • 最新評(píng)論