没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Building Custom PHP
Extensions
Blake Schwendiman
Building Custom PHP Extensions
................................................................................................................I
COPYRIGHT.............................................................................................III
TRADEMARKS..........................................................................................III
ABOUT THE AUTHOR................................................................................III
MOTIVATION FOR THIS DOCUMENT..........................................................................................5
MOTIVATION FOR CREATING YOUR OWN PHP EXTENSION..............................................................5
GETTING STARTED............................................................................................................5
FIRST PHP EXTENSION..............................................................................6
BUILDING THE SHELL.........................................................................................................6
IMPLEMENTING THE EXTENSION............................................................................................12
SUMMARY....................................................................................................................16
BUILDING EXTENSIONS – THE DETAILS.....................................................16
EXT_SKEL....................................................................................................................17
MEMORY MANAGEMENT FUNCTIONS......................................................................................21
DIRECTORY AND FILE FUNCTIONS.........................................................................................22
CONFIG.M4 AND PHP’S AUTOMATIC BUILD SYSTEM...................................................................22
EXPORTED FUNCTIONS DELARATION......................................................................................25
CREATING CONSTANTS.....................................................................................................26
ARGUMENTS AND RETURN VALUES.......................................................................................27
Overview............................................................................................................27
Working with Scalar Types.................................................................................35
Working with Arrays...........................................................................................40
Working with Objects.........................................................................................52
Working with Resources.....................................................................................58
DUPLICATING VARIABLE CONTENTS.......................................................................................63
PRINTING INFORMATION....................................................................................................64
STARTUP AND SHUTDOWN FUNCTIONS...................................................................................67
CALLING USER FUNCTIONS................................................................................................69
INITIALIZATION FILE SUPPORT..............................................................................................73
CLASSES.....................................................................................................................81
FULL EXAMPLE.......................................................................................89
GLPK EXTENSION: CONFIG.M4..........................................................................................91
GLPK EXTENSION: IMPLEMENTATION....................................................................................92
GLPK EXTENSION: SUMMARY..........................................................................................108
SELF-CONTAINED EXTENSIONS..............................................................108
BUILDING EXTENSIONS FOR WINDOWS ..................................................109
BUILDING PHP ON WINDOWS..........................................................................................110
BUILDING A PHP EXTENSION ON WINDOWS..........................................................................114
SUMMARY..................................................................................................................116
CONCLUSION........................................................................................116
APPENDIX A: TABLE OF FIGURES............................................................118
APPENDIX B: TABLE OF TABLES.............................................................119
APPENDIX C: TABLE OF OUTPUT FROM EXAMPLES..................................120
INDEX..................................................................................................121
Page 2
Building Custom PHP Extensions
Copyright
Copyright © 2003, 2009 by Blake Schwendiman. This work is licensed under the
Creative Commons Attribution-Share Alike 3.0 United States License.
Please feel free to pass this ebook along to anyone you feel might use it. Also,
since it was initially written before PHP 5 was available, you may use this book as
a starting point for an updated version – yep, go ahead and change it, make it
better and even sell it, just leave the attribution intact as provided by the license.
Trademarks
The author has made every reasonable attempt to credit the appropriate
trademark and/or registered trademark holders throughout this document.
Windows is a registered trademark of Microsoft Corporation in the United States
and other countries.
About the Author
I am a software developer (a computer nerd at heart). I’m the kid that became so
enamored with the idea of creating computer programs when I was 12 years old
that I made a deal with my dad to split the cost of a new Apple //e as a Christmas
gift. We might have had a game or two, but I spent most of my time teaching
myself BASIC and running such classics as:
1 10 print "Blake "
2 20 goto 10
I graduated from Arizona State University in 1994 with a BS in Computer Science.
I have professionally developed software for Windows, Solaris and UNICOS (Cray)
in C, C++, Ada, Delphi, Visual Basic, and VB.Net (geeks will match the languages
to the OS’s — obviously I didn’t write VB on the Cray).
In about 1999 I was introduced to PHP by a friend while I was developing a basic
ASP application for a web start-up. That introduction literally changed my life. At
the end of 2000, Osbourne/McGraw-Hill published my first PHP programming
guide, PHP 4 Developer’s Guide (now out of print and utterly outdated). I later
self-published two more PHP-related books on Lulu
(http://www.lulu.com/intechrabooks).
You can learn more about me, follow me, friend me, etc. here:
• Blog: http://www.thewhyandthehow.com/
• Wikipedia: http://en.wikipedia.org/wiki/Blake_Schwendiman
• Twitter: http://twitter.com/RealNerd
• Facebook: http://www.facebook.com/profile.php?id=681997232
• Amazon: My Author Page on Amazon
Page 3
Building Custom PHP Extensions
Building Custom PHP Extensions
Motivation for this Document
In the recent past, I have been involved in various projects requiring either the
creation of new PHP extensions or the editing of existing PHP extensions. While
there is some documentation on how to do these tasks, I have found the
documentation to be incomplete, difficult to find, out of date or a combination of
the three. This document is an effort to centralize by own notes and to
(hopefully) provide a tool from which other developers can benefit.
Motivation for Creating Your Own PHP Extension
Most PHP developers will not need to develop their own custom extension. PHP
is a rich programming environment which is constantly updated through a strong
open source development community. However, there are times when
developing a custom extension may be necessary. Additionally, understanding
the extension mechanism in PHP can provide a broader general understanding of
PHP itself.
I have written two custom extensions with practical benefits in the very recent
past. The first was written to map some proprietary C-based financial code into
PHP for an example Intranet-based web site. The second was an implementation
of a TNEF mail attachment decoder for a web-based email project.
Because of the nature of this document, it is assumed that the reader already
has an understanding of PHP development, has built PHP from source and has a
C programming background. You should also be familiar with PHP and its
relationship to the Zend engine. Much of this document assumes you understand
the relationship and so I often do not differentiate between the two in this text.
This is an advanced topic not typical to general PHP programming.
Getting Started
The very first step in developing a new custom extension is to first download and
build PHP from its source. The beginning of this document will focus on building
PHP and the new extension on a Linux system. Later, the Windows build
mechanism will be addressed.
Once you have extracted the PHP source and built it once to ensure that your
system supports the tools needed for a plain-vanilla PHP build, it becomes time
to focus on your new custom extension. To begin, we will develop a simple
extension to show the process and then later build a complex extension to
elaborate the internals and features available to extension builders.
Page 4
Building Custom PHP Extensions
First PHP Extension
Building the Shell
Navigate to the PHP installation directory, then to the ext directory. For
example:
3 cd /path/to/php-4.x.x/ext
Run the ext_skel script in that directory with no parameters. This is the script
that will create the initial files for a new PHP extension. Its parameters are
shown in the table below.
--extname=module module is the name of your extension
--proto=file file contains prototypes of functions to create
--stubs=file Leave out also all module specific stuff and write
just function stubs with function value
declarations and passed argument handling, and
function entries and definitions at the end of the
file, for copying and pasting into an already
existing module.
--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 By default, ext_skel creates both comments in
the source code and a test function to help first
time module writers to get started and testing
configuring and compiling their module. This
option turns off all such things which may just
annoy experienced PHP module coders.
Especially useful with the --stubs parameter.
Table 1: Parameters to the ext_skel script.
NOTE: I suggest you read the README.EXT_SKEL file in the main
PHP install path for details about the script.
For our first extension, we will be creating some simple functions:
double calcpi( int iterations )
This function returns an approximation of pi. The algorithm is a simplistic
random-number method of calculating pi which is neither efficient nor
particularly accurate – but it is fun.
string reverse( string input )
Page 5
剩余121页未读,继续阅读
资源评论
- cangzhu2012-08-29PHP 扩展的资料 英文版的
ghoshi
- 粉丝: 5
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功