为什么要用C扩展
C是静态编译的,执行效率比PHP代码高很多。同样的运算代码,使用C来开发,性能会比PHP要提升数百倍。IO操作如CURL,因为耗时主要在IOWait上,C扩展没有明显优势。
另外C扩展是在进程启动时加载的,PHP代码只能操作Request生命周期的数据,C扩展可操作的范围更广。
源码下载
From PHP官网
http://php.net/downloads.php
From Git
git clone https://github.com/php/php-src.git
From git.php.net
git clone http://git.php.net/repository/php-src.git
From Github
- 打开 php-src project page
- 选择感兴趣的分支
- 点击 Download ZIP即可
这里以zip为例:
wget http://museum.php.net/php5/php-5.3.16.tar.gztar zxvf php-5.3.16.tar.gzmv php-5.3.16 php-src
进入到PHP源码目录:
cd php-src
若使用 Git好处是方便切换不同版本
git clone http://git.php.net/repository/php-src.gitPHP 5.4: git checkout PHP-5.4 PHP 5.5: git checkout PHP-5.5 PHP 5.6: git checkout PHP-5.6 PHP 7.0: git checkout PHP-7.0 PHP HEAD: git checkout master
生成扩展
cd ext
第一步
生成PHP扩展骨架:
./ext_skel --extname=myext
ext_skel是PHP官方提供的用于生成php扩展骨架代码的工具
成功创建完基本文件之后会提示如下:
Creating directory myextCreating basic files: config.m4 config.w32 .svnignore myext.c php_myext.h CREDITS EXPERIMENTAL tests/001.phpt myext.php [done].To use your new extension, you will have to execute the following steps:1. $ cd ..2. $ vi ext/myext/config.m43. $ ./buildconf4. $ ./configure --[with|enable]-myext5. $ make6. $ ./php -f ext/myext/myext.php7. $ vi ext/myext/myext.c8. $ makeRepeat steps 3-6 until you are satisfied with ext/myext/config.m4 andstep 6 confirms that your module is compiled into PHP. Then, start writingcode and repeat the last two steps as often as necessary.
查看生成的文件:
tree myext|-- CREDITS|-- EXPERIMENTAL|-- config.m4|-- config.w32|-- myext.c|-- myext.php|-- php_myext.h`-- tests `-- 001.phpt1 directory, 8 files
config.m4是AutoConf工具的配置文件,用来修改各种编译选项。
第二步
修改 config.m4
cd ..vi ext/myext/config.m4
将
dnl PHP_ARG_WITH(myext, for myext support,dnl Make sure that the comment is aligned:dnl [ --with-myext Include myext support])
修改为
PHP_ARG_WITH(myext, for myext support,[ --with-myext Include myext support])
下边还有一个 –-enable-myext,是表示编译到php内核中。with是作为动态链接库载入的。
第三步
修改 php_myext.h,在45行找到 PHP_FUNCTION(confirm_myext_compiled), 这里就是扩展函数声明部分;
可以增加一行 PHP_FUNCTION(myext_helloworld);, 表示声明了一个myext_helloworld的扩展函数。
第四步
修改 myext.c, 这个是扩展函数的实现部分 在42行之后, 加入 PHP_FE(myext_helloworld, NULL)
const zend_function_entry myext_functions[] = { PHP_FE(confirm_myext_compiled, NULL) /* For testing, remove later. */ PHP_FE(myext_helloworld, NULL) PHP_FE_END /* Must be the last line in myext_functions[] */};
这的代码是将函数指针注册到Zend引擎,增加一行PHP_FE(myext_helloworld, NULL)(后面不要带分号)。
第五步
在 myext.c末尾加myext_helloworld的执行代码。
PHP_FUNCTION(myext_helloworld){ char *arg = NULL; int arg_len, len; char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } php_printf("Hello World!\n"); RETURN_TRUE;}
zend_parse_parameters是用来接受PHP传入的参数,RETURN_XXX宏是用来返回给PHP数据。
第六步
在myext目录下依次执行:
phpize./configuremakemake install
如果执行 ./configure时出现此错误: configure: error: Cannot find php-config. Please use Cwith-php-config=PATH 说明php-config配置文件不在缺省目录里, 重新执行: ./configure --with-php-config=/usr/local/php/bin/php-config节课 这个文件通常是在PHP的安装目录的bi目录下的一个叫做 php-config或者 php-config5的文件
然后修改php.ini加入 extension = "myext.so"
第七步
通过执行 php -r "myext_helloworld('test');"验证扩展是否成功加载,如果成功则输出 hello world!
参考
- http://php.net/git.php
- http://rango.swoole.com/archives/152

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools
