SlideShare a Scribd company logo
Trường đào tạo Lập trình viên Quốc tế 
              AiTi-Aptech




Build your own PHP Extension
           Hanoi PHP Day 2010




                            Bui Dinh Ngoc
                            AiTi-Aptech - CAH
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 ?
1. Buildin PHP function are not enough
2. Existing PHP extension are not enough
3. Pure PHP function are more slow
4. Have C lib can do this for you
Prepare

1. Ubuntu Linux
2. GNU C Compiler , build , make utils
3. PHP 5 Dev package : sudo apt-get install php5-dev
4. PHP source code
    o sudo svn checkout http://svn.php.net/viewvc/php/php-
      src/trunk
  o
PHP-Src-5.3 tree directory
ext_skel.sh script
Write Hello World
Extension
//Example function call
<?php

function hello_world() {
   return 'Hello World';
}

?>
1. Run ext_skel script : sudo ./ext_skel –extname=hello
2.
Result
phpize

The phpize command is used to prepare the build environment
for a PHP extension.
build your own 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

1. sudo ./configure
2. sudo make
3. ls modules -> hello.so
Test

1. Deploy file hello.so
2. Check new extension is loaded by phpinfo function
3. You also can test using existed hello.php script in ext dir
Advance !

1. Build php function with parameter
2. Return value
3. Memory allocation
4. Anti Memory leak
5. Array
6. String
7. Global variable
8. PHP.INI variable
9. ........
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, "ld", &a, &b) == FAILURE) {
      RETURN_NULL();
  }


      RETURN_DOUBLE(a + b);
  }
Return value

1. bool
2. int
3. double      Only 6 return type
4. resource
5. array
6. object
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

1. http://i-php.net/2010/10/t-build-extension-cho-php/
2. http://devzone.zend.com/article/1021
3. "Programming PHP" by Rasmus Lerdorf and Kevin

More Related Content

PPT
Php Ppt
PPT
PDF
Lean Php Presentation
PDF
Ansible is the simplest way to automate. SymfonyCafe, 2015
PPTX
PHP in one presentation
PPT
Advantages of Choosing PHP Web Development
PPTX
Php presentation
PDF
Provisioning with Puppet
Php Ppt
Lean Php Presentation
Ansible is the simplest way to automate. SymfonyCafe, 2015
PHP in one presentation
Advantages of Choosing PHP Web Development
Php presentation
Provisioning with Puppet

What's hot (16)

PPT
GDG DevFest 2013 - PHP Web Apps on Google Cloud
PPT
Cell processor lab
PDF
Puppet Intfrastructure as Code
PDF
DevOpsDaysCPT Ansible Infrastrucutre as Code 2017
PPT
Php hypertext pre-processor
PDF
Infrastructure = Code
PDF
Introducing Ansible
PPTX
How to build a slack-hubot with js
PDF
Ansible Automation to Rule Them All
PPTX
Presentation php
PDF
Remote Control WordPress
PDF
GDG DevFest 2013 - PHP Web Apps on Google Cloud
Cell processor lab
Puppet Intfrastructure as Code
DevOpsDaysCPT Ansible Infrastrucutre as Code 2017
Php hypertext pre-processor
Infrastructure = Code
Introducing Ansible
How to build a slack-hubot with js
Ansible Automation to Rule Them All
Presentation php
Remote Control WordPress
Ad

Similar to build your own php extension (20)

PPT
3. build your own php extension ai ti aptech
PPT
07 build your-own_php_extension
PPT
Build your own PHP extension
PDF
Building Custom PHP Extensions
PDF
第1回PHP拡張勉強会
PDF
Php7 extensions workshop
PPTX
Php extensions
PPTX
Php extensions
KEY
Let's creating your own PHP (tejimaya version)
PDF
Php extensions workshop
PDF
Getting Started with PHP Extensions
PDF
Create your own PHP extension, step by step - phpDay 2012 Verona
PPTX
Extending php (7), the basics
PPT
Extensiones In PHP
PPTX
Php extensions
PDF
PHP selber bauen
PPTX
Php Extensions for Dummies
PDF
Make PHP Extension
PPTX
Introduction to php
PDF
PHP Internals and Virtual Machine
3. build your own php extension ai ti aptech
07 build your-own_php_extension
Build your own PHP extension
Building Custom PHP Extensions
第1回PHP拡張勉強会
Php7 extensions workshop
Php extensions
Php extensions
Let's creating your own PHP (tejimaya version)
Php extensions workshop
Getting Started with PHP Extensions
Create your own PHP extension, step by step - phpDay 2012 Verona
Extending php (7), the basics
Extensiones In PHP
Php extensions
PHP selber bauen
Php Extensions for Dummies
Make PHP Extension
Introduction to php
PHP Internals and Virtual Machine
Ad

More from hazzaz (20)

PPS
Coffee1
PPS
Suy ngam
PPT
Tu dong dat hang tu he thong ban le lon nhat trung quoc
PPT
how startups can benefit from launch community
PPT
social network game
PPTX
trung oss magento overview
PDF
su dung drupal xay dung mang xa hoi
PPT
html5 css3 the future of web technology
PPT
java script unit testing framework
PPT
kiem tien online
PDF
web optimization
PDF
speed up ntvv2 by php ext module
PDF
zingmepracticeforbuildingscalablewebsitewithphp
PPT
mysql optimization
PPT
EAV in Magento
PPT
Albus
PPT
css_trends
PPT
Cloud
PPT
Phan mem tu do nguon mo
PPT
Zing
Coffee1
Suy ngam
Tu dong dat hang tu he thong ban le lon nhat trung quoc
how startups can benefit from launch community
social network game
trung oss magento overview
su dung drupal xay dung mang xa hoi
html5 css3 the future of web technology
java script unit testing framework
kiem tien online
web optimization
speed up ntvv2 by php ext module
zingmepracticeforbuildingscalablewebsitewithphp
mysql optimization
EAV in Magento
Albus
css_trends
Cloud
Phan mem tu do nguon mo
Zing

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Big Data Technologies - Introduction.pptx
PPT
Teaching material agriculture food technology
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
Unlocking AI with Model Context Protocol (MCP)
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Cloud computing and distributed systems.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Approach and Philosophy of On baking technology
“AI and Expert System Decision Support & Business Intelligence Systems”
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation_ Review paper, used for researhc scholars
Review of recent advances in non-invasive hemoglobin estimation
Big Data Technologies - Introduction.pptx
Teaching material agriculture food technology
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
sap open course for s4hana steps from ECC to s4
Unlocking AI with Model Context Protocol (MCP)
Network Security Unit 5.pdf for BCA BBA.
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Cloud computing and distributed systems.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx

build your own php extension