build your own php extension

590 views
498 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
590
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

build your own php extension

  1. 1. Trường đào tạo Lập trình viên Quốc tế  AiTi-AptechBuild your own PHP Extension Hanoi PHP Day 2010 Bui Dinh Ngoc AiTi-Aptech - CAH
  2. 2. PHP Extension ?
  3. 3. PHP Extension• Youve used extensions ?• php_mysql , gd , pdo , curl , ...•
  4. 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. 5. Why and When needPHP extension ?1. Buildin PHP function are not enough2. Existing PHP extension are not enough3. Pure PHP function are more slow4. Have C lib can do this for you
  6. 6. Prepare1. Ubuntu Linux2. GNU C Compiler , build , make utils3. 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
  7. 7. PHP-Src-5.3 tree directory
  8. 8. ext_skel.sh script
  9. 9. Write Hello WorldExtension//Example function call<?phpfunction hello_world() { return Hello World;}?>
  10. 10. 1. Run ext_skel script : sudo ./ext_skel –extname=hello2.
  11. 11. Result
  12. 12. phpizeThe phpize command is used to prepare the build environmentfor a PHP extension.
  13. 13. Edit header file php_hello.h
  14. 14. Insert your functionto header filePHP_FUNCTION(hello); /*My function here*/
  15. 15. 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 inhello_functions[] */};
  16. 16. Implement functionPHP_FUNCTION(hello){php_printf(“Hello, world!n”);}
  17. 17. Build - Run some script1. sudo ./configure2. sudo make3. ls modules -> hello.so
  18. 18. Test1. Deploy file hello.so2. Check new extension is loaded by phpinfo function3. You also can test using existed hello.php script in ext dir
  19. 19. Advance !1. Build php function with parameter2. Return value3. Memory allocation4. Anti Memory leak5. Array6. String7. Global variable8. PHP.INI variable9. ........
  20. 20. Function with parameterfunction hello_add($a, $b) { $sum = (int)$a + (float)$b; return $sum;}
  21. 21. Function with parameterPHP_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); }
  22. 22. Return value1. bool2. int3. double Only 6 return type4. resource5. array6. object
  23. 23. Return value (macro)RETURN_LONG() for integer valuesRETURN_DOUBLE() for floating point valuesRETURN_BOOL() for true/false valuesRETURN_NULL() for null value.....
  24. 24. Memory allocation
  25. 25. 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 cant use wrapper functions
  26. 26. Reference1. 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

×