SlideShare a Scribd company logo
1 of 41
Extending PHP (7.x)
How to, when and when not
Pierre Joye
@pierrejoye
pierre@php.net
http://www.slideshare.net/pierrej
PHP Core developer
Contributors to numerous OSS projects
Portability fan
hiring@BK
K
When to use custom
extensions?
PHP 7 is fast.
PHP will be even faster.
OpCache rocks.
IOs are slow.
Streams are easy.
File Ops are easy.
zval
• Integer > zend_long
• Booleans > types, IS_TRUE/IS_FALSE
• Strings > zend_string
• Float > double
• Object > zend_object
• Resource
Hash Tables
• Array (zval > array > hash table)
• Functions table
• Classes, properties, etc.
Classes
• Classes declaration
Zend/zend_types.h
your best friend.
typedef struct _zval_struct zval;
struct _zval_struct {
zend_value value; /* value */
union {
struct {
ZEND_ENDIAN_LOHI_4(
zend_uchar type, /* active type */
zend_uchar type_flags,
zend_uchar const_flags,
zend_uchar reserved) /* call info for EX(This)*/
} v;
uint32_t type_info;
} u1;
....
};
Hash tables
Hash tables are not arrays!
Hash tables elements can
contain ANYTHING
Zend/zend_hash.h
Your second best buddy
Let get started
Requirements *nix
• Common
• A shell
• editor
• Gcc 5+
• php7-dev
• Autoconf, autotools and co
Requirements Windows
• Common
• A shell
• php-sdk (http://windows.php.net/downloads/php-sdk/)
• vc (14+) for Windows
• php binary tools
(http://windows.php.net/downloads/php-sdk/)
• Setup your sdk structure according to
https://wiki.php.net/internals/windows/stepbystepbuild
Extension directory structure
myext
config.w32
Build script for windows
config.m4
Build script for *nix
php_myext.c
Implementation
php_myext.h
Interface & meta data
Tests
myext tests
myext.phpt
config.w32
// $Id$ // vim:ft=javascript
ARG_ENABLE("myext", "myext support", "yes");
if (PHP_MYEXT == "yes") {
EXTENSION("myext", "php_myext.c");
}
config.m4
dnl Tell PHP about the argument to enable the hello extension
PHP_ARG_ENABLE(myext, Whether to enable the myext extension, [ --enable-myext Enable myext])
if test "$PHP_MYEXT" != "no"; then
PHP_NEW_EXTENSION(myext, php_myext.c, $ext_shared)
fi
Our first function
function myext_hello() {
echo "Hello Manila!";
}
php_myext.h
#define PHP_MYEXT_EXTNAME "myext“
#define PHP_MYEXT_VERSION "0.0.1"
PHP_FUNCTION(myext_hello);
php_myext.c
#include <php.h>
#include "php_myext.h"
include php common interfaces and
types
php_myext.c
Define myext_hello functions and commons
hooks&data for myext
zend_function_entry myext_functions[] = {
PHP_FE(myext_hello, NULL)
PHP_FE_END
};
zend_module_entry myext_module_entry = {
STANDARD_MODULE_HEADER,
PHP_MYEXT_EXTNAME,
myext_functions,
NULL,
NULL, NULL, NULL, NULL,
PHP_MYEXT_VERSION,
STANDARD_MODULE_PROPERTIES,
};
zend_value
Define myext_hello functions and commons
hooks&data for myext
typedef union _zend_value {
zend_long lval; /* long value */
double dval; /* double value */
zend_refcounted *counted;
zend_string *str;
zend_array *arr;
zend_object *obj;
zend_resource *res;
zend_reference *ref;
zend_ast_ref *ast;
zval *zv; void *ptr;
zend_class_entry *ce;
zend_function *func;
struct { uint32_t w1; uint32_t w2; }ww;
} zend_value;
php_myext.c
print „Hello Manilla!n“ to php standard
output
PHP_FUNCTION(myext_hello) {
php_printf("Hello Manila!n");
};
First build
All platforms:
$ phpize
$ configure –enable-myext
For *nix flavors:
$ make
For Windows:
$ nmake
first function, with a string
argument
function myext_print(string $mystring) {
echo "Hello " . $mystring . "!n";
}
first function, with a string
argument
PHP_FUNCTION(myext_print) {
char *mystring;
size_t mystring_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &mystring, &mystring_len) ==
FAILURE) {
return;
}
php_printf("Hello %s!! (%i)n", mystring, mystring_len);
return
}
first function, with an integer
argument
function myext_integer(int $myint) {
if ($myint > 0) {
for ($i = 0; $i < $myint; $i++) {
echo "Hello " . $i . "!n";
}
}
}
first function, with an integer
argument
PHP_FUNCTION(myext_print_integer) {
zend_long lval;
int i;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"l", &lval) == FAILURE) {
return;
}
if (lval > 0) {
for (i=0; i < lval; i++) {
php_printf("Hello %in",i);
}
}
return;
}
first function, with an array
argument
function myext_print_array(array $myarray) {
foreach ($myarray as $val) {
echo "hello ". $val . "n";
}
}
first function, with an array
argument
PHP_FUNCTION(myext_print_array) {
zval *myarray = NULL;
zend_string *key;
zend_ulong num_key;
zval *val;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &myarray) ==
FAILURE) {
return;
}
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(myarray), num_key, key,
val) {
php_printf("Hello %sn", Z_STRVAL_P(val));
}
ZEND_HASH_FOREACH_END();
return;
}
home work
• First class
• Zend Memory Manager
• Use tools like valgrind
• Use external libraries
• Debugging using gdb or vs debugger
resources
• https://wiki.php.net/phpng-upgrading
• https://nikic.github.io/
• https://wiki.php.net/internals/
• http://www.phpinternalsbook.com/
• http://jpauli.github.io/
• https://github.com/php/php-src

More Related Content

What's hot

IoT in Healthcare.pptx
IoT in Healthcare.pptxIoT in Healthcare.pptx
IoT in Healthcare.pptx
Hachmdhmdzad
 
Cloud computing.pptx
Cloud computing.pptxCloud computing.pptx
Cloud computing.pptx
andrewbourget
 

What's hot (20)

Basic Overview Of Cloud Computing
Basic Overview Of Cloud ComputingBasic Overview Of Cloud Computing
Basic Overview Of Cloud Computing
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
 
Metaverse System Architectures
Metaverse System ArchitecturesMetaverse System Architectures
Metaverse System Architectures
 
IoT in Healthcare.pptx
IoT in Healthcare.pptxIoT in Healthcare.pptx
IoT in Healthcare.pptx
 
AI: A Key Enabler for Sustainable Development Goals
AI: A Key Enabler for Sustainable Development GoalsAI: A Key Enabler for Sustainable Development Goals
AI: A Key Enabler for Sustainable Development Goals
 
Internet of things
Internet of thingsInternet of things
Internet of things
 
What is a metaverse?
What is a metaverse?What is a metaverse?
What is a metaverse?
 
Ultimate Guide to Edge Computing!!
Ultimate Guide to Edge Computing!!Ultimate Guide to Edge Computing!!
Ultimate Guide to Edge Computing!!
 
Move Your Desktops and Applications to AWS with Amazon WorkSpaces and AppStre...
Move Your Desktops and Applications to AWS with Amazon WorkSpaces and AppStre...Move Your Desktops and Applications to AWS with Amazon WorkSpaces and AppStre...
Move Your Desktops and Applications to AWS with Amazon WorkSpaces and AppStre...
 
Edge computing
Edge computingEdge computing
Edge computing
 
Edge Computing.pdf
Edge Computing.pdfEdge Computing.pdf
Edge Computing.pdf
 
IoT Meets the Cloud: The Origins of Edge Computing
IoT Meets the Cloud:  The Origins of Edge ComputingIoT Meets the Cloud:  The Origins of Edge Computing
IoT Meets the Cloud: The Origins of Edge Computing
 
Internet Of Things Introduction
Internet Of Things Introduction Internet Of Things Introduction
Internet Of Things Introduction
 
From gaming to the metaverse
From gaming to the metaverseFrom gaming to the metaverse
From gaming to the metaverse
 
IoT Developer Survey 2016
IoT Developer Survey 2016IoT Developer Survey 2016
IoT Developer Survey 2016
 
Edge Computing and 5G - SDN/NFV London meetup
Edge Computing and 5G - SDN/NFV London meetupEdge Computing and 5G - SDN/NFV London meetup
Edge Computing and 5G - SDN/NFV London meetup
 
What is private cloud Explained
What is private cloud ExplainedWhat is private cloud Explained
What is private cloud Explained
 
Cloud computing
Cloud computingCloud computing
Cloud computing
 
Cloud computing.pptx
Cloud computing.pptxCloud computing.pptx
Cloud computing.pptx
 
The Five Biggest Tech Trends Transforming Government In 2022
The Five Biggest Tech Trends Transforming Government In 2022The Five Biggest Tech Trends Transforming Government In 2022
The Five Biggest Tech Trends Transforming Government In 2022
 

Similar to Extending php (7), the basics

Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for Dummies
Elizabeth Smith
 
PHP7 - The New Engine for old good train
PHP7 - The New Engine for old good trainPHP7 - The New Engine for old good train
PHP7 - The New Engine for old good train
Xinchen Hui
 
Decoupled Libraries for PHP
Decoupled Libraries for PHPDecoupled Libraries for PHP
Decoupled Libraries for PHP
Paul Jones
 
The Php Life Cycle
The Php Life CycleThe Php Life Cycle
The Php Life Cycle
Xinchen Hui
 
Php opcodes sep2008
Php opcodes sep2008Php opcodes sep2008
Php opcodes sep2008
bengiuliano
 
External Language Stored Procedures for MySQL
External Language Stored Procedures for MySQLExternal Language Stored Procedures for MySQL
External Language Stored Procedures for MySQL
Antony T Curtis
 

Similar to Extending php (7), the basics (20)

Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for Dummies
 
SyScan Singapore 2010 - Returning Into The PHP-Interpreter
SyScan Singapore 2010 - Returning Into The PHP-InterpreterSyScan Singapore 2010 - Returning Into The PHP-Interpreter
SyScan Singapore 2010 - Returning Into The PHP-Interpreter
 
Php extensions
Php extensionsPhp extensions
Php extensions
 
"Развитие ветки PHP-7"
"Развитие ветки PHP-7""Развитие ветки PHP-7"
"Развитие ветки PHP-7"
 
Как мы сделали PHP 7 в два раза быстрее PHP 5 / Дмитрий Стогов (Zend Technolo...
Как мы сделали PHP 7 в два раза быстрее PHP 5 / Дмитрий Стогов (Zend Technolo...Как мы сделали PHP 7 в два раза быстрее PHP 5 / Дмитрий Стогов (Zend Technolo...
Как мы сделали PHP 7 в два раза быстрее PHP 5 / Дмитрий Стогов (Zend Technolo...
 
Writing MySQL UDFs
Writing MySQL UDFsWriting MySQL UDFs
Writing MySQL UDFs
 
Zf2 phpquebec
Zf2 phpquebecZf2 phpquebec
Zf2 phpquebec
 
PHP7 - The New Engine for old good train
PHP7 - The New Engine for old good trainPHP7 - The New Engine for old good train
PHP7 - The New Engine for old good train
 
Decoupled Libraries for PHP
Decoupled Libraries for PHPDecoupled Libraries for PHP
Decoupled Libraries for PHP
 
The Php Life Cycle
The Php Life CycleThe Php Life Cycle
The Php Life Cycle
 
Fatc
FatcFatc
Fatc
 
A quick start on Zend Framework 2
A quick start on Zend Framework 2A quick start on Zend Framework 2
A quick start on Zend Framework 2
 
Php opcodes sep2008
Php opcodes sep2008Php opcodes sep2008
Php opcodes sep2008
 
External Language Stored Procedures for MySQL
External Language Stored Procedures for MySQLExternal Language Stored Procedures for MySQL
External Language Stored Procedures for MySQL
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing Insanity
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redis
 
Php7 extensions workshop
Php7 extensions workshopPhp7 extensions workshop
Php7 extensions workshop
 
Php’s guts
Php’s gutsPhp’s guts
Php’s guts
 
Zend Framework
Zend FrameworkZend Framework
Zend Framework
 

More from Pierre Joye

Webdevcon Keynote hh-2012-09-18
Webdevcon Keynote hh-2012-09-18Webdevcon Keynote hh-2012-09-18
Webdevcon Keynote hh-2012-09-18
Pierre Joye
 
Intro ipcberlin2012
Intro ipcberlin2012Intro ipcberlin2012
Intro ipcberlin2012
Pierre Joye
 
Php symfony and software lifecycle
Php symfony and software lifecyclePhp symfony and software lifecycle
Php symfony and software lifecycle
Pierre Joye
 

More from Pierre Joye (18)

Php 7.x 8.0 and hhvm and
Php 7.x 8.0 and hhvm and Php 7.x 8.0 and hhvm and
Php 7.x 8.0 and hhvm and
 
Php7 hhvm and co
Php7 hhvm and coPhp7 hhvm and co
Php7 hhvm and co
 
Php 7 hhvm and co
Php 7 hhvm and coPhp 7 hhvm and co
Php 7 hhvm and co
 
Php core. get rid of bugs and contribute
Php core. get rid of bugs and contributePhp core. get rid of bugs and contribute
Php core. get rid of bugs and contribute
 
Webdevcon Keynote hh-2012-09-18
Webdevcon Keynote hh-2012-09-18Webdevcon Keynote hh-2012-09-18
Webdevcon Keynote hh-2012-09-18
 
Devcon hh-2012
Devcon hh-2012Devcon hh-2012
Devcon hh-2012
 
Short Intro talk to IPC/Berlin 2012
Short Intro talk to IPC/Berlin 2012Short Intro talk to IPC/Berlin 2012
Short Intro talk to IPC/Berlin 2012
 
Intro ipcberlin2012
Intro ipcberlin2012Intro ipcberlin2012
Intro ipcberlin2012
 
Webdevcon pierrejoye-php54-and-other
Webdevcon pierrejoye-php54-and-otherWebdevcon pierrejoye-php54-and-other
Webdevcon pierrejoye-php54-and-other
 
Php symfony and software lifecycle
Php symfony and software lifecyclePhp symfony and software lifecycle
Php symfony and software lifecycle
 
Mongodb - drupal dev days
Mongodb - drupal dev daysMongodb - drupal dev days
Mongodb - drupal dev days
 
Webplatform And Php
Webplatform And PhpWebplatform And Php
Webplatform And Php
 
Keynote, PHP World Kongress Munich
Keynote, PHP World Kongress MunichKeynote, PHP World Kongress Munich
Keynote, PHP World Kongress Munich
 
Php On Windows
Php On WindowsPhp On Windows
Php On Windows
 
Php On Windows Internals
Php On Windows InternalsPhp On Windows Internals
Php On Windows Internals
 
Test Fest 2009
Test Fest 2009Test Fest 2009
Test Fest 2009
 
PHP Worl Kongress Munich
PHP Worl Kongress MunichPHP Worl Kongress Munich
PHP Worl Kongress Munich
 
Developing PHP internals on Windows
Developing PHP internals on WindowsDeveloping PHP internals on Windows
Developing PHP internals on Windows
 

Recently uploaded

Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 

Recently uploaded (20)

Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 

Extending php (7), the basics