07 build your-own_php_extension

Download 07 build your-own_php_extension

Post on 24-May-2015




0 download

Embed Size (px)


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