build your own php extension hanoi php day 2010 bui dinh ngoc aiti-aptech - cah trưng đào to lp...
TRANSCRIPT
![Page 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/1.jpg)
Build your own PHP Extension
Hanoi PHP Day 2010
Bui Dinh NgocAiTi-Aptech - CAH
Trường đào tạo Lập trình viên Quốc tế AiTi-Aptech
![Page 2: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/2.jpg)
PHP Extension ?
![Page 3: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/3.jpg)
PHP Extension
• You've used extensions ?• php_mysql , gd , pdo , curl , ...•
![Page 4: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/4.jpg)
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
![Page 5: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/5.jpg)
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 slow4. Have C lib can do this for you
![Page 6: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/6.jpg)
Prepare
1. Ubuntu Linux 2. GNU C Compiler , build , make utils 3. PHP 5 Dev package : sudo apt-get install php5-dev4. PHP source code
o sudo svn checkout http://svn.php.net/viewvc/php/php-src/trunk
o
![Page 7: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/7.jpg)
PHP-Src-5.3 tree directory
![Page 8: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/8.jpg)
ext_skel.sh script
![Page 9: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/9.jpg)
Write Hello World Extension//Example function call <?php
function hello_world() { return 'Hello World';}
?>
![Page 10: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/10.jpg)
1. Run ext_skel script : sudo ./ext_skel –extname=hello2.
![Page 11: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/11.jpg)
Result
![Page 12: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/12.jpg)
phpize
The phpize command is used to prepare the build environment for a PHP extension.
![Page 13: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/13.jpg)
![Page 14: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/14.jpg)
Edit header file php_hello.h
![Page 15: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/15.jpg)
Insert your function to header filePHP_FUNCTION(hello); /*My function here*/
![Page 16: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/16.jpg)
Edit C source file - pre declareconst 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[] */};
![Page 17: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/17.jpg)
Implement function
PHP_FUNCTION(hello){php_printf(“Hello, world!\n”);}
![Page 18: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/18.jpg)
Build - Run some script
1. sudo ./configure2. sudo make3. ls modules -> hello.so
![Page 19: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/19.jpg)
Test
1. Deploy file hello.so2. Check new extension is loaded by phpinfo function 3. You also can test using existed hello.php script in ext dir
![Page 20: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/20.jpg)
Advance !
1. Build php function with parameter2. Return value3. Memory allocation 4. Anti Memory leak5. Array6. String7. Global variable8. PHP.INI variable9. ........
![Page 21: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/21.jpg)
Function with parameter
function hello_add($a, $b) {
$sum = (int)$a + (float)$b;
return $sum;}
![Page 22: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/22.jpg)
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); }
![Page 23: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/23.jpg)
Return value
1. bool2. int3. double4. resource5. array6. object
Only 6 return type
![Page 24: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/24.jpg)
Return value (macro)
RETURN_LONG() for integer valuesRETURN_DOUBLE() for floating point valuesRETURN_BOOL() for true/false valuesRETURN_NULL() for null value.....
![Page 25: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/25.jpg)
Memory allocation
![Page 26: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/26.jpg)
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
![Page 27: 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](https://reader033.vdocuments.mx/reader033/viewer/2022051400/5513df825503466f748b53af/html5/thumbnails/27.jpg)
Reference
1. http://i-php.net/2010/10/t-build-extension-cho-php/2. http://devzone.zend.com/article/10213. "Programming PHP" by Rasmus Lerdorf and Kevin