PHP扩展开发(一)
首先需要确定系统中安装了gcc编译器,合适版本的bison等,下面是从源码编译安装PHP需要执行的基本命令:
# cd php-src# ./buildconf# ./configure --enable-debug --enable-maintainer-zts --enable-cli# make# make install
构建一个基本的扩展骨架
在PHP扩展开发时,使用ext_skel完成扩展的结构骨架创建。
$ ./ext_skel./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]] [--skel=dir] [--full-xml] [--no-help] --extname=module 这里的module是要创建的扩展名称 --proto=file 这里的file文件包含了要创建的函数的原型 --stubs=file generate only function stubs in file --xml generate xml documentation to be added to phpdoc-cvs --skel=dir 创建扩展骨架的目录 --full-xml generate xml documentation for a self-contained extension (not yet implemented) --no-help don't try to be nice and create comments in the code and helper functions to test if the module compiled
注意: ext_skel命令文件在源文件的ext目录下。
这里的--extname参数是要创建的扩展名称,扩展名称为 小写字母 + 下划线 组成,并且,
在ext目录中必须是唯一的。
例如,这里要创建一个名为ext_demo_1的PHP扩展:
/vagrant/ext$ ./ext_skel --extname=ext_demo_1Creating directory ext_demo_1Creating basic files: config.m4 config.w32 .svnignore ext_demo_1.c php_ext_demo_1.h CREDITS EXPERIMENTAL tests/001.phpt ext_demo_1.php [done].To use your new extension, you will have to execute the following steps:
$ cd ..
$ vi ext/extdemo1/config.m4
$ ./buildconf
$ ./configure --[with|enable]-extdemo1
$ make
$ ./php -f ext/extdemo1/extdemo1.php
$ vi ext/extdemo1/extdemo1.c
$ make
Repeat steps 3-6 until you are satisfied with ext/extdemo1/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.
现在,在ext目录下出现了一个新建的扩展目录ext_demo_1:
/vagrant/ext/ext_demo_1$ lsconfig.m4 CREDITS ext_demo_1.c php_ext_demo_1.hconfig.w32 EXPERIMENTAL ext_demo_1.php tests
这时,该扩展是无法编译通过的,需要先编辑config.m4文件才行。
配置文件config.m4
配置文件config.m4告诉UNIX构建系统扩展支持的configure选项以及扩展需要的额外的库,
包含哪些源文件等,该文件使用的是GNU的autoconf语法,以dnl开头的行为注释,使用中括号([和])包含的为字符串。
autoconf语法参见 AUTOCONF文档
PHP_ARG_ENABLE(ext_demo_1, whether to enable ext_demo_1 support,[ --enable-ext_demo_1 Enable ext_demo_1 support])if test "$PHP_EXT_DEMO_1" != "no"; then PHP_SUBST(EXT_DEMO_1_SHARED_LIBADD) PHP_NEW_EXTENSION(ext_demo_1, ext_demo_1.c, $ext_shared)fi
上述为autoconf的配置文件,第一个宏PHP_ARG_ENABLE,含有三个参数:
extdemo1 这是第一个参数,为./configure建立了名为enable-ext_demo_1的选项
第二个参数将会在./configure命令处理到该扩展的配置文件时,显示该参数的内容
第三个参数是./configure命令的帮助,在使用./configure --help的时候显示
第二个宏为PHP_NEW_EXTENSION,该宏声明了扩展的模块和必须要编译作为扩展一部分的源码文件。
如果需要多个源文件,则使用空格分隔,第三个参数$ext_shared与调用
PHP_SUBST(EXT_DEMO_1_SHARED_LIBADD)有关。
PHP_NEW_EXTENSION(ext_demo_1, ext_demo_1.c, $ext_shared)
编译扩展
修改完config.m4文件之后,接下来编译PHP和扩展。
/vagrant$ ./configure --disable-libxml --enable-ext_demo_1 --disable-dom --disable-simplexml --disable-xml --disable-xmlreader --disable-xmlwriter --without-pear --prefix=/usr/local/php/vagrant$ make/vagrant$ sudo make installInstalling PHP SAPI module: cgiInstalling PHP CGI binary: /usr/local/php/bin/Installing PHP CLI binary: /usr/local/php/bin/Installing PHP CLI man page: /usr/local/php/man/man1/Installing build environment: /usr/local/php/lib/php/build/Installing header files: /usr/local/php/include/php/Installing helper programs: /usr/local/php/bin/ program: phpize program: php-configInstalling man pages: /usr/local/php/man/man1/ page: phpize.1 page: php-config.1/vagrant/build/shtool install -c ext/phar/phar.phar /usr/local/php/binln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/pharInstalling PDO headers: /usr/local/php/include/php/ext/pdo/
此时,PHP安装在了/usr/local/php目录下,进入该目录,可以看到如下文件:
/usr/local/php$ lsbin include lib man
进入/usr/local/php/bin目录,执行以下命令:
/usr/local/php/bin$ ./php --info|grep demoConfigure Command => './configure' '--disable-libxml' '--enable-ext_demo_1' '--disable-dom' '--disable-simplexml' '--disable-xml' '--disable-xmlreader' '--disable-xmlwriter' '--without-pear' '--prefix=/usr/local/php'ext_demo_1ext_demo_1 support => enabled
可以看到,phpinfo()中扩展支持已经启用了,按照上述步骤安装的扩展中包含了一个测试扩展是否能够正常工作的函数,
该函数名为confirm_ext_demo_1_compiled(arg),执行结果如下:
/usr/local/php/bin$ ./php -r "echo confirm_ext_demo_1_compiled('mylxsw');"Congratulations! You have successfully modified ext/ext_demo_1/config.m4. Module mylxsw is now compiled into PHP.
可以看到,ext_demo_1扩展安装成功了,关于扩展开发更多内容,请移步 AICODE.CC.

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

会话劫持可以通过以下步骤实现:1.获取会话ID,2.使用会话ID,3.保持会话活跃。在PHP中防范会话劫持的方法包括:1.使用session_regenerate_id()函数重新生成会话ID,2.通过数据库存储会话数据,3.确保所有会话数据通过HTTPS传输。

PHP中有四种主要错误类型:1.Notice:最轻微,不会中断程序,如访问未定义变量;2.Warning:比Notice严重,不会终止程序,如包含不存在文件;3.FatalError:最严重,会终止程序,如调用不存在函数;4.ParseError:语法错误,会阻止程序执行,如忘记添加结束标签。

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

HTTP请求方法包括GET、POST、PUT和DELETE,分别用于获取、提交、更新和删除资源。1.GET方法用于获取资源,适用于读取操作。2.POST方法用于提交数据,常用于创建新资源。3.PUT方法用于更新资源,适用于完整更新。4.DELETE方法用于删除资源,适用于删除操作。

在PHP中,应使用password_hash和password_verify函数实现安全的密码哈希处理,不应使用MD5或SHA1。1)password_hash生成包含盐值的哈希,增强安全性。2)password_verify验证密码,通过比较哈希值确保安全。3)MD5和SHA1易受攻击且缺乏盐值,不适合现代密码安全。

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

箭头函数在PHP7.4中引入,是短闭包的简化形式。1)它们使用=>运算符定义,省略function和use关键字。2)箭头函数自动捕获当前作用域变量,无需use关键字。3)它们常用于回调函数和短小计算,提高代码简洁性和可读性。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7
