Your SlideShare is downloading. ×
0
Build your own PHP Extension Hanoi PHP Day 2010                      Bui Dinh Ngoc AiTi-Aptech - CAH Trường đào tạo Lập tr...
PHP Extension ?
PHP Extension <ul><ul><li>You've used extensions ? </li></ul></ul><ul><ul><li>php_mysql , gd , pdo , curl ,  ... </li></ul...
PHP Extension  (Zend Engine) <ul><ul><li>PHP language  written in   C   </li></ul></ul><ul><ul><li>PHP interpreter written...
Why and When need  PHP extension ? <ul><ul><li>Buildin PHP function are not enough  </li></ul></ul><ul><ul><li>Existing PH...
Prepare  <ul><ul><li>Ubuntu Linux  </li></ul></ul><ul><ul><li>GNU C Compiler , build , make utils   </li></ul></ul><ul><ul...
PHP-Src-5.3 tree directory <ul><li>  </li></ul>
ext_skel.sh script <ul><li>  </li></ul>
Write Hello World  Extension <ul><li>//Example function call   </li></ul><ul><li><?php function   hello_world () {     ret...
  <ul><ul><li>Run ext_skel script : sudo ./ext_skel –extname=hello </li></ul></ul><ul><ul><li>  </li></ul></ul>
Result <ul><li>  </li></ul>
phpize <ul><li>The  phpize  command is used to prepare the build environment for a PHP extension. </li></ul><ul><li>  </li...
  <ul><li>  </li></ul>
Edit header file php_hello.h
Insert your function  to header file <ul><li>PHP_FUNCTION(hello);  /*My function here*/ </li></ul>
Edit C source file  - pre declare <ul><li>const zend_function_entry simhash_functions[] = { PHP_FE(confirm_hello_compiled,...
Implement function <ul><li>PHP_FUNCTION(hello) { php_printf(“Hello, world!n”); } </li></ul>
Build - Run some script <ul><ul><li>sudo ./configure </li></ul></ul><ul><ul><li>sudo make </li></ul></ul><ul><ul><li>ls mo...
Test <ul><ul><li>Deploy file hello.so </li></ul></ul><ul><ul><li>Check new extension is loaded by phpinfo function  </li><...
Advance ! <ul><ul><li>Build php function with parameter </li></ul></ul><ul><ul><li>Return value </li></ul></ul><ul><ul><li...
Function with parameter <ul><li>function hello_add ( $a ,   $b ) {      $sum   = (int) $a   + (float) $b ;     return  $su...
Function with parameter <ul><li>PHP_FUNCTION(hello_add) {     long a;     double b;       if ( zend_parse_parameters (ZEND...
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>...
Return value (macro) <ul><li>RETURN_LONG() for integer values </li></ul><ul><li>RETURN_DOUBLE() for floating point values ...
Memory allocation <ul><li>  </li></ul>
Anti Memory leak <ul><ul><li>In C, memory management always very hard  . </li></ul></ul><ul><ul><li>Wrapper functions prov...
Reference <ul><ul><li>http://i-php.net/2010/10/t-build-extension-cho-php/ </li></ul></ul><ul><ul><li>http://devzone.zend.c...
Upcoming SlideShare
Loading in...5
×

Build your own PHP extension

2,847

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,847
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
40
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "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  http://svn.php.net/viewvc/php/php-src/trunk </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. ext_skel.sh 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 -> hello.so </li></ul></ul>
  19. 19. Test <ul><ul><li>Deploy file hello.so </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>http://i-php.net/2010/10/t-build-extension-cho-php/ </li></ul></ul><ul><ul><li>http://devzone.zend.com/article/1021 </li></ul></ul><ul><ul><li>&quot;Programming PHP&quot; by Rasmus Lerdorf and Kevin  </li></ul></ul><ul><li>  </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×