Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

07 build your-own_php_extension


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

07 build your-own_php_extension

  1. 1. Build your own PHP Extension Hanoi PHP Day 2010                      Bui Dinh Ngoc AiTi-Aptech - CAH Trường đào tạo Lập trình viên Quốc tế  AiTi-Aptech
  2. 2. PHP Extension ?
  3. 3. PHP Extension <ul><ul><li>You've used extensions ? </li></ul></ul><ul><ul><li>php_mysql , gd , pdo , curl ,  ... </li></ul></ul><ul><ul><li>  </li></ul></ul>
  4. 4. PHP Extension  (Zend Engine) <ul><ul><li>PHP language  written in  C </li></ul></ul><ul><ul><li>PHP interpreter written in  C too </li></ul></ul><ul><ul><li>And PHP Extension must   written in  C </li></ul></ul><ul><ul><li>Another PHP implement may be using diffrence language </li></ul></ul>
  5. 5. Why and When need  PHP extension ? <ul><ul><li>Buildin PHP function are not enough  </li></ul></ul><ul><ul><li>Existing PHP extension are not enough </li></ul></ul><ul><ul><li>Pure PHP function are more slow </li></ul></ul><ul><ul><li>Have C lib can do this for you </li></ul></ul>
  6. 6. Prepare  <ul><ul><li>Ubuntu Linux  </li></ul></ul><ul><ul><li>GNU C Compiler , build , make utils  </li></ul></ul><ul><ul><li>  PHP 5 Dev package : sudo apt-get install php5-dev </li></ul></ul><ul><ul><li>PHP source code  </li></ul></ul><ul><ul><ul><li>sudo svn checkout </li></ul></ul></ul><ul><ul><ul><li>  </li></ul></ul></ul>
  7. 7. PHP-Src-5.3 tree directory <ul><li>  </li></ul>
  8. 8. script <ul><li>  </li></ul>
  9. 9. Write Hello World  Extension <ul><li>//Example function call </li></ul><ul><li><?php function hello_world () {     return 'Hello World' ; } ?> </li></ul>
  10. 10.   <ul><ul><li>Run ext_skel script : sudo ./ext_skel –extname=hello </li></ul></ul><ul><ul><li>  </li></ul></ul>
  11. 11. Result <ul><li>  </li></ul>
  12. 12. phpize <ul><li>The phpize command is used to prepare the build environment for a PHP extension. </li></ul><ul><li>  </li></ul><ul><li>  </li></ul>
  13. 13.   <ul><li>  </li></ul>
  14. 14. Edit header file php_hello.h
  15. 15. Insert your function  to header file <ul><li>PHP_FUNCTION(hello);  /*My function here*/ </li></ul>
  16. 16. Edit C source file  - pre declare <ul><li>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[] */ }; </li></ul>
  17. 17. Implement function <ul><li>PHP_FUNCTION(hello) { php_printf(“Hello, world!n”); } </li></ul>
  18. 18. Build - Run some script <ul><ul><li>sudo ./configure </li></ul></ul><ul><ul><li>sudo make </li></ul></ul><ul><ul><li>ls modules -> </li></ul></ul>
  19. 19. Test <ul><ul><li>Deploy file </li></ul></ul><ul><ul><li>Check new extension is loaded by phpinfo function  </li></ul></ul><ul><ul><li>You also can test using existed hello.php script in ext dir </li></ul></ul>
  20. 20. Advance ! <ul><ul><li>Build php function with parameter </li></ul></ul><ul><ul><li>Return value </li></ul></ul><ul><ul><li>Memory allocation  </li></ul></ul><ul><ul><li>Anti Memory leak </li></ul></ul><ul><ul><li>Array </li></ul></ul><ul><ul><li>String </li></ul></ul><ul><ul><li>Global variable </li></ul></ul><ul><ul><li>PHP.INI variable </li></ul></ul><ul><ul><li>........ </li></ul></ul>
  21. 21. Function with parameter <ul><li>function hello_add ( $a , $b ) {      $sum = (int) $a + (float) $b ;     return $sum ; } </li></ul>
  22. 22. Function with parameter <ul><li>PHP_FUNCTION(hello_add) {     long a;     double b;      if ( zend_parse_parameters (ZEND_NUM_ARGS() TSRMLS_CC, &quot; ld&quot;, &a, &b ) == FAILURE) {         RETURN_NULL();     }             RETURN_DOUBLE(a + b);     } </li></ul>
  23. 23. Return value <ul><ul><li>bool </li></ul></ul><ul><ul><li>int </li></ul></ul><ul><ul><li>double </li></ul></ul><ul><ul><li>resource </li></ul></ul><ul><ul><li>array </li></ul></ul><ul><ul><li>object </li></ul></ul>Only 6 return type
  24. 24. Return value (macro) <ul><li>RETURN_LONG() for integer values </li></ul><ul><li>RETURN_DOUBLE() for floating point values </li></ul><ul><li>RETURN_BOOL() for true/false values </li></ul><ul><li>RETURN_NULL() for null value </li></ul><ul><li>..... </li></ul><ul><li>  </li></ul>
  25. 25. Memory allocation <ul><li>  </li></ul>
  26. 26. Anti Memory leak <ul><ul><li>In C, memory management always very hard  . </li></ul></ul><ul><ul><li>Wrapper functions provides you with a safety net and some helpful debugging facilities </li></ul></ul><ul><ul><li>But convert existing C source can't use wrapper functions </li></ul></ul><ul><li>  </li></ul>
  27. 27. Reference <ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li>&quot;Programming PHP&quot; by Rasmus Lerdorf and Kevin  </li></ul></ul><ul><li>  </li></ul>