VC 建立php扩展
软件 php的源文件和安装包要一致
php5.3.8(VC9 x86 Thread Safe)
php5.3.8源文件(tar.bz2)
VC
bison.exe
MSYS(MSYS类似于Cygwin,但是由于工作原理的不同,速度更快、体积更小、功能强大、便于携带http://code.google.com/p/msys-cn/)
因为我的开发只是一个很简单的demo,没有使用第三方了类库。如果是把linux下拿过来的扩展项目,可能用到一些库。可能用cygwin会比较好。但是没有cygwin完全可以在window下开发。
1。解压下载到的源文件包tar.bz2包到C盘c:/phpsrc,并且解压php安装包(VC9 x86Thread Safe,也就是能够正常使用的php压缩包文件)到C:/php,我们只需要里面的一个文件C:/php/dev/ php5.lib,复制php5.lib到c:/phpsrc。
2。复制bison.exe到Microsoft Visual Studio\Common\MSDev98\Bin把Microsoft Visual Studio\Common\MSDev98\Bin的绝对路径添加到windows环境变量
3 .在这里我们开始生成生成config.w32.h。CMD在里面操作
进入:c:/phpsrc执行
C:\phpsrc>buildconf Rebuilding configure.js Now run 'configure --help'
?建立一个临时环境变量
C:\phpsrc>set path=%path%;C:/phpsrc/bin C:\phpsrc>cscript /nologo configure.js --with-php-build="../phpsrc" --without-libxml --disable-odbc
如果想要Non Thread Safe 模式就去掉上面的命令最后的参数--disable-zts
Saving configure options to config.nice.bat Checking for cl.exe ... <in default path> Detected compiler MSVC9 (Visual C++ 2008) Detected 32-bit compiler Checking for link.exe ... C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN Checking for nmake.exe ... <in default path> Checking for lib.exe ... <in default path> ...........中间省略............. ------------------------------------------- | | | ------------------------------------------- | Build type | Release | | Thread Safety | Yes | | Compiler | MSVC6 (Visual C++ 6.0) | | Architecture | x86 | ------------------------------------------- Type 'nmake' to build PHP</in></in></in>
如果出现上类似的提示, 说明你的PHP开发环境已经搭建成功,同时在main下面多了一个 config.w32.h。即PHP开发环境的配置文件。如果没有这个脚本,windows下开发php,简直太悲惨了。
?
4.开发PHP扩展的方法
到这里我们就可以建立开发PHP扩展了,但是无从下手,我们该怎么办,其实PHP也给我们提供了很好用的工具来建立一个PHP扩展的骨架。就是用C:\phpsrc\ext下的ext_skel_win32.php如何运行,以php结尾,说明依赖php。下面看具体方法,我建立一个名为’test‘的扩展。这里要确保你的php.exe可以直接在cmd下使用,具体还是加入Path路径 。虽然有ext_skel_win32.php,用php执行,但是我们不知道具体的内容。 其实执行如下命令:
C:\phpsrc\ext>php ext_skel_win32.php 'sh' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
ext_skel_win32.php源码中说要使用cygwin,但我机器上没有装cygwin,另外发现其中实际上只使用到了sh,而我机器上装的MSYS里也有sh,应该可以用的吧,于是就将ext_skel_win32.php中的$cygwin_path变量设置成了MSYS的BIN目录
$cygwin_path = 'c:\msys\1.0\bin';?
C:\phpsrc\ext>php ext_skel_win32.php ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]] [--skel=dir] [--full-xml] [--no-help] --extname=module module is the name of your extension --proto=file file contains prototypes of functions to create --stubs=file generate only function stubs in file --xml generate xml documentation to be added to phpdoc-cvs --skel=dir path to the skeleton directory --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 PHP Warning: fopen(/.php): failed to open stream: No such file or directory in C:\phpsrc\ext\ext_skel_win32.php on line 52 C:\phpsrc\ext>
?可以看到如何使用这php脚本。大体命令如下
c:\phpsrc\ext>php ext_skel_win32.php --extname=test Creating directory test Creating basic files: config.m4 config.w32 .svnignore test.c php_test.h CREDITS EXPERIMENTAL tests/001.phpt test.php [do ne]. To use your new extension, you will have to execute the following steps: 1. $ cd .. 2. $ vi ext/test/config.m4 3. $ ./buildconf 4. $ ./configure --[with|enable]-test 5. $ make 6. $ ./php -f ext/test/test.php 7. $ vi ext/test/test.c 8. $ make Repeat steps 3-6 until you are satisfied with ext/test/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. c:\phpsrc\ext>
这样就成功创建了一个php扩展骨架 ,你可以在里面修改。具体的还要好好研究PHP API。具体扩展的位置就在ext目录下,打开可以看到test文件夹,这就是刚才命令创建的。
C:\phpsrc\ext\test 的目录 2011-11-29 16:57 <dir> . 2011-11-29 16:57 <dir> .. 2011-11-29 16:57 16 .svnignore 2011-11-29 16:57 1,970 config.m4 2011-11-29 16:57 282 config.w32 2011-11-29 16:57 4 CREDITS 2011-11-29 16:57 0 EXPERIMENTAL 2011-11-29 16:57 2,768 php_test.h 2011-11-29 16:57 5,128 test.c 2011-11-29 16:57 4,954 test.dsp 2011-11-29 16:57 500 test.php 2011-11-29 16:57 <dir> tests 9 个文件 15,622 字节 3 个目录 7,441,883,136 可用字节 C:\phpsrc\ext\test></dir> </dir> </dir>
这样一个php扩展的框架已经创建完成了。
?
下面就是配置使用vc++6开发这个扩展
III. 添加依赖的php5ts.lib
? 在php的二进制包中的 dev目录下将 php5ts.lib 拷到我们的test目录中, 否则编译将通不过。
IV. 添加test c代码
生成的test目录中有关键文件包括
? test.dsp,
? test.c,
? php_test.h,
其他文件暂时不必关心.
提示:切忌test目录不可以挪移出ext目录,否则会编译报缺少php.h.
1. 修改php_test.h
扩展的新函数: 在PHP_FUNCTION(confirm_test_compiled); 行后添加一行
PHP_FUNCTION(confirm_test_compiled); PHP_FUNCTION(test); // 新增的行
2. 修改test.c
在PHP_FUNCTION(confirm_test_compiled) 后添加我们的新函数
PHP_FUNCTION(test){ php_printf("Hello C extension"); }
在数组zend_function_entry test_functions[]增加一行
const zend_function_entry test_functions[] = { PHP_FE(confirm_test_compiled, NULL) /* For testing, remove later. */ PHP_FE(test, NULL) // 新增的行 PHP_FE_END /* Must be the last line in test_functions[] */ };
?V. 构建DLL文件
用vc6打开我们的工程,就是test.dsp
1. 修改编译方式为release: 选择Build->Set Active Configuration设置默认编译方式为Release, 否则会提示缺少php5ts_debug.lib ,其实就是php5ts.lib。
2. 按F5编译。会在ext上级的Release_TS目录下生成php_test.dll
提示:如果愿意使用命令行编译也是可以的,命令如下:
msdev test\test.dsp /MAKE "test - Win32 Release_TS"
?VI. 集成dll到php中。
?

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

드림위버 CS6
시각적 웹 개발 도구

WebStorm Mac 버전
유용한 JavaScript 개발 도구
