[轉(zhuǎn)一篇比較老的文章]編寫自己的php擴(kuò)展函數(shù)
Jun 21, 2016 am 09:15 AM比較|函數(shù)
作者: 飄在四方
本人還沒測(cè)試過,有興趣的可以測(cè)試下
Yorgo Sun 2002/01/22
php程序?qū)懙臅r(shí)間長(zhǎng)了,自然對(duì)他所提供的功能了如指掌,他所提供的一大堆功能,真是覺得很好用,但有時(shí)候會(huì)發(fā)現(xiàn)php也缺少一些功能,自己總是會(huì)產(chǎn)生為php添加一些自定義的功能的想法。久而久之,終于今天憋不住了,開始動(dòng)手研究如何添加。
下載一個(gè)php的源代碼包,這里使用的是php 4.0.5版,解壓后會(huì)看到php的根目錄下會(huì)有README.EXT_SKEL這樣一個(gè)文件,打開詳細(xì)閱讀了一下,發(fā)現(xiàn)了一個(gè)非常好用的工具,這個(gè)工具可以幫你構(gòu)建一個(gè)空的php擴(kuò)展,然后你向里面添加相應(yīng)的代碼就可以完成你自己的功能擴(kuò)展了。下面我們就來介紹如何使用這個(gè)工具。
首先轉(zhuǎn)移你的目錄到php的目錄下的ext目錄,如果你只需要一個(gè)基本的擴(kuò)展框架的話,執(zhí)行下面的命令:
./ext_skel --extname=module_name
module_name是你自己可以選擇的擴(kuò)展模塊的名字,例如我選擇的my_module。執(zhí)行工具后會(huì)自動(dòng)在ext目錄下建立你選擇的module_name名字的目錄,里面已經(jīng)生成了相關(guān)的代碼,這些代碼中只需要調(diào)整config.m4文件中的三行注釋就可以正常的編譯帶這個(gè)自定義擴(kuò)展模塊的php了。在php的根目錄執(zhí)行下列操作就可以得到。
./buildconf
./configure --enable-module_name
make
下面我來演示建立my_module擴(kuò)展框架的全過程,為了更有效果,我們來完成一個(gè)php的擴(kuò)展功能,在php中調(diào)用這個(gè)功能可以在web頁面中顯示hello world這個(gè)經(jīng)典單詞。
在php目錄下的ext目錄中,執(zhí)行下面的命令
./ext_skel --extname=my_module
得到反饋結(jié)果:
Creating directory my_module
Creating basic files: config.m4 Makefile.in .cvsignore my_module.c php_my_module.h tests/001.phpt my_module.php [done].
To use your new extension, you will have to execute the following steps:
1. $ cd ..
2. $ vi ext/my_module/config.m4
3. $ ./buildconf
4. $ ./configure --[with|enable]-my_module
5. $ make
6. $ ./php -f ext/my_module/my_module.php
7. $ vi ext/my_module/my_module.c
8. $ make
Repeat steps 3-6 until you are satisfied with ext/my_module/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.
如果你能看懂上面的東西,那就照著去做。如果不是太明白的話,按照我下面的提示來做也可以。
Cd my_module
首先進(jìn)入my_module目錄
vi config.m4
使用文本編輯器打開config.m4文件,文件內(nèi)容大致如下:
dnl $Id$
dnl config.m4 for extension my_module
dnl don't forget to call PHP_EXTENSION(my_module)
dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.
dnl If your extension references something external, use with:
dnl PHP_ARG_WITH(my_module, for my_module support,
dnl Make sure that the comment is aligned:
dnl [ --with-my_module Include my_module support])
dnl Otherwise use enable:
dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,
dnl Make sure that the comment is aligned:
dnl [ --enable-my_module Enable my_module support])
if test "$PHP_MY_MODULE" != "no"; then
dnl If you will not be testing anything external, like existence of
dnl headers, libraries or functions in them, just uncomment the
dnl following line and you are ready to go.
dnl Write more examples of tests here...
PHP_EXTENSION(my_module, $ext_shared)
Fi
根據(jù)你自己的選擇將
dnl PHP_ARG_WITH(my_module, for my_module support,
dnl Make sure that the comment is aligned:
dnl [ --with-my_module Include my_module support])
修改成
PHP_ARG_WITH(my_module, for my_module support,
Make sure that the comment is aligned:
[ --with-my_module Include my_module support])
或者將
dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,
dnl Make sure that the comment is aligned:
dnl [ --enable-my_module Enable my_module support])
修改成
PHP_ARG_ENABLE(my_module, whether to enable my_module support,
Make sure that the comment is aligned:
[ --enable-my_module Enable my_module support])
一般我會(huì)選擇后者,然后保存退出。如果你對(duì)vi文本編輯器的操作有困難的話,請(qǐng)參考相應(yīng)的說明文章,這里就不再詳細(xì)描述了。
Vi my_module.c
將文件其中的下列代碼進(jìn)行修改
/* Every user visible function must have an entry in my_module_functions[].
*/
function_entry my_module_functions[] = {
PHP_FE(say_hello, NULL) /* ?添加著一行代碼 */
PHP_FE(confirm_my_module_compiled, NULL) /* For testing, remove later. */
{NULL, NULL, NULL} /* Must be the last line in my_module_functions[] */
};
在文件的最后添加下列代碼
PHP_FUNCTION(say_hello)
{
zend_printf("hello world\n");
}
保存文件退出
vi php_my_module.h
在文件中PHP_FUNCTION(confirm_my_module_compiled);一行前面添加下面的代碼
PHP_FUNCTION(say_hello);
保存文件退出
退回到php的根目錄下,執(zhí)行下面的命令
./buildconf
./configure --enable-my_module
make
如果一切順利的話,我們現(xiàn)在已經(jīng)將擴(kuò)展模塊my_module編譯到php里面了。我們編寫下面的代碼進(jìn)行測(cè)試
Say_hello();
?>
保存文件為say_hello.php
在php的根目錄下運(yùn)行
./php –q say_hello.php
正常情況下會(huì)顯示
hello world
表示我們的第一個(gè)擴(kuò)展正常的運(yùn)行了!
解釋一下上面做的操作,ext_skel生成一些框下文件,我們需要修改以下文件
my_module.c 擴(kuò)展模塊的主程序
php_my_module.h 擴(kuò)展模塊的頭文件
config.m4 配置文件
主程序中描述了php擴(kuò)展模塊的聲明,模塊中含有多少個(gè)函數(shù),各個(gè)函數(shù)的作用,在phpinfo函數(shù)中顯示什么內(nèi)容,模塊初始化做些什么,結(jié)束做些什么都會(huì)在這個(gè)文件里進(jìn)行描述。我們?cè)谏厦嬷皇翘砑恿艘粋€(gè)函數(shù)say_hello,并且描述了say_hello函數(shù)的具體內(nèi)容,調(diào)用zend_printf系統(tǒng)函數(shù)在php中打印字符串。
在對(duì)應(yīng)的頭文件中聲明了say_hello這個(gè)函數(shù),從而完成了我們預(yù)期的功能。下面我們會(huì)編寫一個(gè)更復(fù)雜的擴(kuò)展,創(chuàng)造一個(gè)帶參數(shù)的php擴(kuò)展函數(shù),根據(jù)給入的參數(shù),顯示hello world, xxxx。Xxxx代表輸入的字符串內(nèi)容,例如我的名字yorgo。
Vi my_module.c
修改最后的say_hello函數(shù)內(nèi)容如下:
PHP_FUNCTION(say_hello)
{
zval **yourname;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &yourname) == FAILURE)
{
WRONG_PARAM_COUNT;
}
zend_printf("hello world, %s\n", Z_STRVAL_PP(yourname));
}
存盤退出。
退回php的根目錄,運(yùn)行
make
修改say_hello.php為
Say_hello(“yorgo”);
?>
保存退出后運(yùn)行
./php –q say_hello.php
得出結(jié)果
hello world, yorgo
表示我們這次的修改也成功了,可以改變say_hello中的參數(shù),看看動(dòng)態(tài)的效果。
這里主要解釋上面修改的函數(shù)內(nèi)容,由于say_hello函數(shù)需要有參數(shù)引入,所以在my_module.c中的say_hello函數(shù)主要在進(jìn)行參數(shù)的處理,將php中引用say_hello時(shí)所填寫的參數(shù)內(nèi)容正確的傳遞到my_module.c中的say_hello處理函數(shù)中。為此,程序中添加了這么幾行。
zval **yourname;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &yourname) == FAILURE)
{
WRONG_PARAM_COUNT;
}
zend_printf("hello world, %s\n", Z_STRVAL_PP(yourname));
代碼解釋如下:
zval **yourname;
初始化一個(gè)參數(shù)的指針
ZEND_NUM_ARGS()
得到傳遞過來得參數(shù)數(shù)量,并且判斷如果不為1的時(shí)候表示有問題,報(bào)錯(cuò)。
zend_get_parameters_ex(1, &yourname)
將剛剛初始化的指針指向傳遞過來的參數(shù),如果不成功則報(bào)錯(cuò)。
Z_STRVAL_PP(yourname)
處理指針指向的參數(shù)并獲得實(shí)際存儲(chǔ)的值。
(待續(xù))

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

Select the appropriate AI voice recognition service and integrate PHPSDK; 2. Use PHP to call ffmpeg to convert recordings into API-required formats (such as wav); 3. Upload files to cloud storage and call API asynchronous recognition; 4. Analyze JSON results and organize text using NLP technology; 5. Generate Word or Markdown documents to complete the automation of meeting records. The entire process needs to ensure data encryption, access control and compliance to ensure privacy and security.
