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
PHP Extension ?
PHP Extension You've used extensions ? php_mysql , gd , pdo , curl ,  ...  
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
Why and When need  PHP extension ? Buildin PHP function are not enough  Existing PHP extension are not enough  Pure PHP function are more slow Have C lib can do this for you
Prepare  Ubuntu Linux  GNU C Compiler , build , make utils     PHP 5 Dev package : sudo apt-get install php5-dev PHP source code  sudo svn checkout  http://svn.php.net/viewvc/php/php-src/trunk  
PHP-Src-5.3 tree directory  
ext_skel.sh script  
Write Hello World  Extension //Example function call   <?php function   hello_world () {     return   'Hello World' ; } ?>
  Run ext_skel script : sudo ./ext_skel –extname=hello  
Result  
phpize The  phpize  command is used to prepare the build environment for a PHP extension.    
   
Edit header file php_hello.h
Insert your function  to header file PHP_FUNCTION(hello);  /*My function here*/
Edit C source file  - pre declare 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[] */ };
Implement function PHP_FUNCTION(hello) { php_printf(“Hello, world!\n”); }
Build - Run some script sudo ./configure sudo make ls modules ->  hello.so
Test Deploy file hello.so Check new extension is loaded by phpinfo function  You also can test using existed hello.php script in ext dir
Advance ! Build php function with parameter Return value Memory allocation  Anti Memory leak Array String Global variable PHP.INI variable ........
Function with parameter function hello_add ( $a ,   $b ) {      $sum   = (int) $a   + (float) $b ;     return  $sum ; }
Function with parameter 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);     }
Return value bool int double resource array object Only 6 return type
Return value (macro) RETURN_LONG() for integer values RETURN_DOUBLE() for floating point values RETURN_BOOL() for true/false values RETURN_NULL() for null value .....   
Memory allocation  
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   
Reference http://i-php.net/2010/10/t-build-extension-cho-php/ http://devzone.zend.com/article/1021 &quot;Programming PHP&quot; by Rasmus Lerdorf and Kevin    

07 build your-own_php_extension

  • 1.
    Build your ownPHP 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.
  • 3.
    PHP Extension You'veused extensions ? php_mysql , gd , pdo , curl ,  ...  
  • 4.
    PHP Extension  (ZendEngine) 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.
    Why and Whenneed  PHP extension ? Buildin PHP function are not enough  Existing PHP extension are not enough Pure PHP function are more slow Have C lib can do this for you
  • 6.
    Prepare  Ubuntu Linux GNU C Compiler , build , make utils    PHP 5 Dev package : sudo apt-get install php5-dev PHP source code  sudo svn checkout  http://svn.php.net/viewvc/php/php-src/trunk  
  • 7.
  • 8.
  • 9.
    Write Hello World Extension //Example function call <?php function hello_world () {     return 'Hello World' ; } ?>
  • 10.
      Run ext_skelscript : sudo ./ext_skel –extname=hello  
  • 11.
  • 12.
    phpize The phpize command is used to prepare the build environment for a PHP extension.    
  • 13.
  • 14.
    Edit header filephp_hello.h
  • 15.
    Insert your function to header file PHP_FUNCTION(hello);  /*My function here*/
  • 16.
    Edit C sourcefile  - pre declare 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[] */ };
  • 17.
    Implement function PHP_FUNCTION(hello){ php_printf(“Hello, world!\n”); }
  • 18.
    Build - Runsome script sudo ./configure sudo make ls modules -> hello.so
  • 19.
    Test Deploy filehello.so Check new extension is loaded by phpinfo function  You also can test using existed hello.php script in ext dir
  • 20.
    Advance ! Buildphp function with parameter Return value Memory allocation  Anti Memory leak Array String Global variable PHP.INI variable ........
  • 21.
    Function with parameterfunction hello_add ( $a , $b ) {      $sum = (int) $a + (float) $b ;     return $sum ; }
  • 22.
    Function with parameterPHP_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);     }
  • 23.
    Return value boolint double resource array object Only 6 return type
  • 24.
    Return value (macro)RETURN_LONG() for integer values RETURN_DOUBLE() for floating point values RETURN_BOOL() for true/false values RETURN_NULL() for null value .....  
  • 25.
  • 26.
    Anti Memory leakIn 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  
  • 27.