Download presentation
Presentation is loading. Please wait.
Published byAustin Robertson Modified over 11 years ago
1
Build your own PHP Extension Hanoi PHP Day 2010 Bui Dinh Ngoc AiTi-Aptech - CAH Trưng đào to Lp trình viên Quc t AiTi-Aptech
2
PHP Extension ?
3
PHP Extension You've used extensions ? php_mysql, gd, pdo, curl,...
4
PHP Extension (Zend Engine) PHP language written in C PHP interpreter written in C too And PHP Extension must written in C Another PHP implement may be using diffrence language
5
Why and When need PHP extension ? 1.Buildin PHP function are not enough 2.Existing PHP extension are not enough 3.Pure PHP function are more slow 4.Have C lib can do this for you
6
Prepare 1.Ubuntu Linux 2.GNU C Compiler, build, make utils 3. PHP 5 Dev package : sudo apt-get install php5-dev 4.PHP source code o sudo svn checkout http://svn.php.net/viewvc/php/php- src/trunk o
7
PHP-Src-5.3 tree directory
8
ext_skel.sh script
9
Write Hello World Extension //Example function call
10
1.Run ext_skel script : sudo./ext_skel –extname=hello 2.
11
Result
12
phpize The phpize command is used to prepare the build environment for a PHP extension.
14
Edit header file php_hello.h
15
Insert your function to header file PHP_FUNCTION(hello); /*My function here*/
16
Edit C source file - pre declare const zend_function_entry simhash_functions[] = { PHP_FE(confirm_hello_compiled, NULL) /* For testing, remove later. */ PHP_FE(hello, NULL) {NULL, NULL, NULL} /* Must be the last line in hello_functions[] */ };
17
Implement function PHP_FUNCTION(hello) { php_printf(Hello, world!\n); }
18
Build - Run some script 1.sudo./configure 2.sudo make 3.ls modules -> hello.so
19
Test 1.Deploy file hello.so 2.Check new extension is loaded by phpinfo function 3.You also can test using existed hello.php script in ext dir
20
Advance ! 1.Build php function with parameter 2.Return value 3.Memory allocation 4.Anti Memory leak 5.Array 6.String 7.Global variable 8.PHP.INI variable 9.........
21
Function with parameter function hello_add($a, $b) { $sum = (int)$a + (float)$b; return $sum; }
22
Function with parameter PHP_FUNCTION(hello_add) { long a; double b; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ld", &a, &b) == FAILURE) { RETURN_NULL(); } RETURN_DOUBLE(a + b); }
23
Return value 1.bool 2.int 3.double 4.resource 5.array 6.object Only 6 return type
24
Return value (macro) RETURN_LONG() for integer values RETURN_DOUBLE() for floating point values RETURN_BOOL() for true/false values RETURN_NULL() for null value.....
25
Memory allocation
26
Anti Memory leak In C, memory management always very hard. Wrapper functions provides you with a safety net and some helpful debugging facilities But convert existing C source can't use wrapper functions
27
Reference 1.http://i-php.net/2010/10/t-build-extension-cho-php/ 2.http://devzone.zend.com/article/1021 3."Programming PHP" by Rasmus Lerdorf and Kevin
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.