网上的教程是比较多的,但是我发现在windows下的扩展开发比较少,而且大多都是php5.3版本以前的,今天我就给大家讲解一下php扩展开发,我就拿php5.5的版本来说明一下的了
windows环境(我个人的)
windows 8.1 企业版(mac os Boot Camp安装的)
Visual Studio 2012 版本
msysgit (http://msysgit.github.io/ 下载)
php-sdk-binary-tools-20110915.zip (http://windows.php.net/downloads/php-sdk/下载)
deps-5.5-vc11-x86.7z (http://windows.php.net/downloads/php-sdk/下载)
php-5.5.20 (http://php.net/downloads.php下载,这个版本不定自己选择)
第一步
解压php-sdk-binary-tools的二进制包,譬如我解压到我的C:\php-sdk文件夹中,现在的目录结构如下
C:\php-sdk
--bin
--script
--share
然后,这个是你已经安装完成了visual studio 2012,打开VS2012 Native Tools Command Prompt命令行工具
#进入目录
cd C:\php-sdk
#设置环境变量
bin\phpsdk_setvars.bat
#创建常用的php-sdk目录
bin\phpsdk_buildtree.bat phpdev
如果我们打开bin\phpsdk_buildtree.bat文件就会发现它只创建到VC9,没有VC11,但是我们如果开发php5.5版本,我们需要的是VC11,这时候我们就要把C:\php-sdk\phpdev\vc9复制一份到C:\php-sdk\phpdev\vc11,现在的目录结构如下:
C:\php-sdk\phpdev\
--vc6
--vc8
--vc9
--vc11
因为我下载的deps-5.5-vc11-x86.7z,所以我要解压deps-5.5-vc11-x86.7z到C:\php-sdk\phpdev\vc11\x86\deps文件夹下覆盖,里边都是我们需要的库文件和一些必要的工具等等。
然后,将我们下载的php-5.5.20.tar.bz2解压到C:\php-sdk\phpdev\vc11\x86\php-5.5.20文件夹中。
编译安装php
回到VS2012 Native Tools Command Prompt
#进入php源目录文件夹
cd c:\php-sdk\phpdev\vc11\x86\php-5.5.20
buildconf
#查看带的扩展和编译命令
configure --help
php-sdk
如果你没有安装php,现在这里就帮助到你了,可以先编译安装一下php
configure --disable-all --enable-cli
然后,你会看到Type 'nmake' to build PHP,然后编译
nmake
在C:\php-sdk\phpdev\vc11\x86\php-5.5.20\Release_TS文件夹下就生成了php.exe文件,环境变量中加入这个路径,好在命令行中能使用php命令。
开发PHP的第一个扩展
假如我们要开发一个widuu()函数,实现的效果如下
function widuu($string){
return "your first extension {$string} is ok";
}
进入扩展目录,生成扩展文件夹
cd C:\php-sdk\phpdev\vc11\x86\php-5.5.20\ext
#输入php ext_skel_win32.php --extname=扩展名称
php ext_skel_win32.php --extname=widuuweb
这时候我们在C:\php-sdk\phpdev\vc11\x86\php-5.5.20\ext就看到了自己的目录widuuweb,打开widuuweb\php_widuuweb.h,在
#PHP_MINFO_FUNCTION(widuuweb);下一行写入自己的函数,譬如我定义的是widuu()函数
PHP_FUNCTION(widuu);
打开php_widuuweb.c编写函数,在PHP_FUNCTION(confirm_widuuweb_compiled)下
PHP_FUNCTION(widuu){
char *arg_string = NULL;
int arg_len,str_len;
char *string;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"s",&arg_string,&arg_len) == FAILURE){
return;
}
str_len = spprintf(&string,0,"your first extension %.78s is ok",arg_string);
RETURN_STRINGL(string,str_len,0);
}
然后找到PHP_FE(confirm_widuuweb_compiled, NULL),在下边注册你的函数
const zend_function_entry widuuweb_functions[] = {
PHP_FE(confirm_widuuweb_compiled, NULL) /* For testing, remove later. */
PHP_FE(widuu,NULL)
PHP_FE_END /* Must be the last line in widuuweb_functions[] */
};
修改C:\php-sdk\phpdev\vc11\x86\php-5.5.20\ext\widuuweb\config.w32,去掉
// ARG_ENABLE("widuuweb", "enable widuuweb support", "no");
前边的注释,vs命令行,输入cd .. 到php-5.5.20目录中,输入下面的命令
buildconf --force
#查看扩展
configure --help
如果,有错误,大家可以打开,目录下configure.js查看,我这个出现了错误,是configure.js的4791行,
ARG_ENABLE("widuuweb", "enable widuuweb support", "no"); */
多了一个*/的注释,去掉就可以了,然后再输入
configure --help
就看到了,有一行
--enable-widuuweb enable widuuweb support
然后配置
configure --disable-all --enable-cli --enable-widuuweb=shared
php动态扩展
其中--enable-widuuweb=shared动态扩展,正好与静态扩展相对。
然后输入nmake,完成后,我们在C:\php-sdk\phpdev\vc11\x86\php-5.5.20\Release_TS目录下就发现了php_widuuweb.dll文件,可以放到环境中测试了。
如果你本地没有安装php环境,而是跟我一样只是编译了php,如果测试,我建议静态编译看效果
configure --disable-all --enable-cli --enable-widuuweb=static
nmake
然后,你可以再Release_TS目录下,输入
php -r "echo widuu('widuu');" //输出"your first extension widuu is ok"
ok,就说到这里,以后再慢慢来讲,有什么不懂的大家可以留言,基础的ZEND_API,大家可以去walu.cc查看

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc


Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
