SlideShare a Scribd company logo
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    

More Related Content

PPT
build your own php extension
PDF
Introduction to PHP
PDF
Introduction to PHP - Basics of PHP
PPTX
Licão 14 debug script
PPTX
Introduction to PHP
PPTX
Writing php extensions in golang
PDF
PHP Jump Start
KEY
Kansai.pm 10周年記念 Plack/PSGI 入門
build your own php extension
Introduction to PHP
Introduction to PHP - Basics of PHP
Licão 14 debug script
Introduction to PHP
Writing php extensions in golang
PHP Jump Start
Kansai.pm 10周年記念 Plack/PSGI 入門

What's hot (18)

PPTX
Design patterns as power of programing
PDF
Even better debugging; Equipped yourself with powerful tools.
PDF
Php extensions workshop
PDF
PSGI REST API
PDF
Php7 extensions workshop
ODP
Perl In The Command Line
PDF
Php under the_hood
PDF
Portland Puppet User Group June 2014: Writing and publishing puppet modules
PDF
PHP 7 new engine
PDF
Trick or Tip - Symfony Edition
PPTX
Python Programming Essentials - M35 - Iterators & Generators
PDF
Quick tour of PHP from inside
PPTX
Php7 HHVM and co
PDF
Profiling php5 to php7
ODP
Perl one-liners
PPT
Cell processor lab
PDF
Mysqlnd, an unknown powerful PHP extension
Design patterns as power of programing
Even better debugging; Equipped yourself with powerful tools.
Php extensions workshop
PSGI REST API
Php7 extensions workshop
Perl In The Command Line
Php under the_hood
Portland Puppet User Group June 2014: Writing and publishing puppet modules
PHP 7 new engine
Trick or Tip - Symfony Edition
Python Programming Essentials - M35 - Iterators & Generators
Quick tour of PHP from inside
Php7 HHVM and co
Profiling php5 to php7
Perl one-liners
Cell processor lab
Mysqlnd, an unknown powerful PHP extension
Ad

Similar to 3. build your own php extension ai ti aptech (20)

PDF
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
PDF
The new features of PHP 7
ODP
PHP: The easiest language to learn.
PPT
GDG DevFest 2013 - PHP Web Apps on Google Cloud
PPT
Php Tutorial
PDF
Living With Legacy Code
PDF
Behavior & Specification Driven Development in PHP - #OpenWest
PDF
Advanced Eclipse Workshop (held at IPC2010 -spring edition-)
PDF
Diving into HHVM Extensions (PHPNW Conference 2015)
PDF
Diving into HHVM Extensions (Brno PHP Conference 2015)
PPTX
PPTX
Zephir - A Wind of Change for writing PHP extensions
PPT
PDF
Lean Php Presentation
PDF
ElePHPant7 - Introduction to PHP7
PPT
PPS
Simplify your professional web development with symfony
PDF
Giới thiệu PHP 7
PDF
Winter%200405%20-%20Beginning%20PHP
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
The new features of PHP 7
PHP: The easiest language to learn.
GDG DevFest 2013 - PHP Web Apps on Google Cloud
Php Tutorial
Living With Legacy Code
Behavior & Specification Driven Development in PHP - #OpenWest
Advanced Eclipse Workshop (held at IPC2010 -spring edition-)
Diving into HHVM Extensions (PHPNW Conference 2015)
Diving into HHVM Extensions (Brno PHP Conference 2015)
Zephir - A Wind of Change for writing PHP extensions
Lean Php Presentation
ElePHPant7 - Introduction to PHP7
Simplify your professional web development with symfony
Giới thiệu PHP 7
Winter%200405%20-%20Beginning%20PHP
Ad

More from Quang Anh Le (20)

PDF
Mobilemonday.vn mobile interaction - mr ng cuu long goldsun focus media
PPTX
Mobilemonday.vn adflex
PPTX
Mobilemonday.vn momo-monetize thinking_tu do tai chinh_v1.2
PDF
02 mobile monday hanoi april 2013 vinova
PPT
01 mobile monday hanoi april 2013 mobile game - afoli
PPTX
03 mobile monday hanoi april 2013 monetize thinking g mo m-work
PPT
2. minh chau momo t4
PPT
0. mobilemonday.vn hcm 20130424
PPT
Vietnam mobile day 2012 phan mem cho phai dep - mana
PPTX
Vietnam mobile day 2012 i os memory management - techmaster
PPTX
Vietnam mobile day 2012 monetizing mobile apps games - rubycell
PDF
Vietnam mobile day 2012 cuoc cach mang thoi ky hau mobile - misfit
PPTX
Vietnam mobile day 2012 multi platform development - game loft
PDF
Vietnam mobile day 2012 developer opportunity - nokia
PPT
Vietnam mobile day 2012 building a mobile linux firmware for your router - ...
PPT
Vietnam mobile day 2012 ban quyen mobile contents - vsm
PDF
Vietnam mobile day 2012 platform mo va ung dung de phat trien mang xa hoi m...
PPT
Vietnam mobile day 2012 co hoi mobile tai vietnam - cyber agent
PPTX
Vietnam mobile day 2012 html5 game using impact js - aiti-aptech
PPTX
Vietnam mobile day 2012 tuong lai cho nha phat trien nds - share plus
Mobilemonday.vn mobile interaction - mr ng cuu long goldsun focus media
Mobilemonday.vn adflex
Mobilemonday.vn momo-monetize thinking_tu do tai chinh_v1.2
02 mobile monday hanoi april 2013 vinova
01 mobile monday hanoi april 2013 mobile game - afoli
03 mobile monday hanoi april 2013 monetize thinking g mo m-work
2. minh chau momo t4
0. mobilemonday.vn hcm 20130424
Vietnam mobile day 2012 phan mem cho phai dep - mana
Vietnam mobile day 2012 i os memory management - techmaster
Vietnam mobile day 2012 monetizing mobile apps games - rubycell
Vietnam mobile day 2012 cuoc cach mang thoi ky hau mobile - misfit
Vietnam mobile day 2012 multi platform development - game loft
Vietnam mobile day 2012 developer opportunity - nokia
Vietnam mobile day 2012 building a mobile linux firmware for your router - ...
Vietnam mobile day 2012 ban quyen mobile contents - vsm
Vietnam mobile day 2012 platform mo va ung dung de phat trien mang xa hoi m...
Vietnam mobile day 2012 co hoi mobile tai vietnam - cyber agent
Vietnam mobile day 2012 html5 game using impact js - aiti-aptech
Vietnam mobile day 2012 tuong lai cho nha phat trien nds - share plus

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PDF
KodekX | Application Modernization Development
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
Teaching material agriculture food technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Machine learning based COVID-19 study performance prediction
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
cuic standard and advanced reporting.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
Encapsulation theory and applications.pdf
KodekX | Application Modernization Development
The Rise and Fall of 3GPP – Time for a Sabbatical?
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
sap open course for s4hana steps from ECC to s4
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Teaching material agriculture food technology
Review of recent advances in non-invasive hemoglobin estimation
Machine learning based COVID-19 study performance prediction
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Dropbox Q2 2025 Financial Results & Investor Presentation
Per capita expenditure prediction using model stacking based on satellite ima...
Chapter 3 Spatial Domain Image Processing.pdf
Big Data Technologies - Introduction.pptx
cuic standard and advanced reporting.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Building Integrated photovoltaic BIPV_UPV.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Programs and apps: productivity, graphics, security and other tools

3. build your own php extension ai ti aptech

  • 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
  • 3. PHP Extension You've used extensions ? php_mysql , gd , pdo , curl ,  ...  
  • 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. 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
  • 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  
  • 9. Write Hello World  Extension //Example function call <?php function hello_world () {     return 'Hello World' ; } ?>
  • 10.   Run ext_skel script : sudo ./ext_skel –extname=hello  
  • 12. phpize The phpize command is used to prepare the build environment for a PHP extension.    
  • 13.    
  • 14. Edit header file php_hello.h
  • 15. Insert your function  to header file PHP_FUNCTION(hello);  /*My function here*/
  • 16. 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[] */ };
  • 17. Implement function PHP_FUNCTION(hello) { php_printf(“Hello, world!\n”); }
  • 18. Build - Run some script sudo ./configure sudo make ls modules -> hello.so
  • 19. 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
  • 20. Advance ! Build php function with parameter Return value Memory allocation  Anti Memory leak Array String Global variable PHP.INI variable ........
  • 21. Function with parameter function hello_add ( $a , $b ) {      $sum = (int) $a + (float) $b ;     return $sum ; }
  • 22. 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);     }
  • 23. Return value bool int 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 .....  
  • 26. 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