SlideShare a Scribd company logo
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.
Extending php (7), the basics
Extending php (7), the basics
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
Extending php (7), the basics
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

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

PDF
Php7 extensions workshop
julien pauli
 
PDF
Php extensions workshop
julien pauli
 
PPTX
Php extensions
Elizabeth Smith
 
PPTX
Php extensions
Elizabeth Smith
 
PPTX
Php extensions
Elizabeth Smith
 
PDF
Create your own PHP extension, step by step - phpDay 2012 Verona
Patrick Allaert
 
PPTX
Php Extensions for Dummies
Elizabeth Smith
 
PDF
PHP Internals and Virtual Machine
julien pauli
 
PPTX
Zend Framework Workshop
10n Software, LLC
 
PDF
第1回PHP拡張勉強会
Ippei Ogiwara
 
PDF
Building Custom PHP Extensions
AzRy LLC, Caucasus School of Technology
 
PPT
3. build your own php extension ai ti aptech
Quang Anh Le
 
PPT
Build your own PHP extension
Võ Duy Tuấn
 
PPT
07 build your-own_php_extension
Nguyen Duc Phu
 
PPTX
Php’s guts
Elizabeth Smith
 
PPT
build your own php extension
hazzaz
 
PPT
PHPBootcamp - Zend Framework
thomasw
 
PDF
Workshop quality assurance for php projects - phpbelfast
Michelangelo van Dam
 
PDF
Getting Started with PHP Extensions
MichaelBrunoLochemem
 
PPTX
PHP in 2018 - Q1 - AFUP Limoges
✅ William Pinaud
 
Php7 extensions workshop
julien pauli
 
Php extensions workshop
julien pauli
 
Php extensions
Elizabeth Smith
 
Php extensions
Elizabeth Smith
 
Php extensions
Elizabeth Smith
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Patrick Allaert
 
Php Extensions for Dummies
Elizabeth Smith
 
PHP Internals and Virtual Machine
julien pauli
 
Zend Framework Workshop
10n Software, LLC
 
第1回PHP拡張勉強会
Ippei Ogiwara
 
Building Custom PHP Extensions
AzRy LLC, Caucasus School of Technology
 
3. build your own php extension ai ti aptech
Quang Anh Le
 
Build your own PHP extension
Võ Duy Tuấn
 
07 build your-own_php_extension
Nguyen Duc Phu
 
Php’s guts
Elizabeth Smith
 
build your own php extension
hazzaz
 
PHPBootcamp - Zend Framework
thomasw
 
Workshop quality assurance for php projects - phpbelfast
Michelangelo van Dam
 
Getting Started with PHP Extensions
MichaelBrunoLochemem
 
PHP in 2018 - Q1 - AFUP Limoges
✅ William Pinaud
 

More from Pierre Joye (16)

PPTX
Php7 hhvm and co
Pierre Joye
 
PPTX
Php 7 hhvm and co
Pierre Joye
 
PPTX
Php core. get rid of bugs and contribute
Pierre Joye
 
PPTX
Webdevcon Keynote hh-2012-09-18
Pierre Joye
 
PPTX
Devcon hh-2012
Pierre Joye
 
PPTX
Short Intro talk to IPC/Berlin 2012
Pierre Joye
 
PPTX
Intro ipcberlin2012
Pierre Joye
 
PPTX
Webdevcon pierrejoye-php54-and-other
Pierre Joye
 
PPTX
Php symfony and software lifecycle
Pierre Joye
 
PPTX
Mongodb - drupal dev days
Pierre Joye
 
PPTX
Webplatform And Php
Pierre Joye
 
PPTX
Keynote, PHP World Kongress Munich
Pierre Joye
 
PPTX
Php On Windows
Pierre Joye
 
PPTX
Test Fest 2009
Pierre Joye
 
PPT
PHP Worl Kongress Munich
Pierre Joye
 
PPT
Developing PHP internals on Windows
Pierre Joye
 
Php7 hhvm and co
Pierre Joye
 
Php 7 hhvm and co
Pierre Joye
 
Php core. get rid of bugs and contribute
Pierre Joye
 
Webdevcon Keynote hh-2012-09-18
Pierre Joye
 
Devcon hh-2012
Pierre Joye
 
Short Intro talk to IPC/Berlin 2012
Pierre Joye
 
Intro ipcberlin2012
Pierre Joye
 
Webdevcon pierrejoye-php54-and-other
Pierre Joye
 
Php symfony and software lifecycle
Pierre Joye
 
Mongodb - drupal dev days
Pierre Joye
 
Webplatform And Php
Pierre Joye
 
Keynote, PHP World Kongress Munich
Pierre Joye
 
Php On Windows
Pierre Joye
 
Test Fest 2009
Pierre Joye
 
PHP Worl Kongress Munich
Pierre Joye
 
Developing PHP internals on Windows
Pierre Joye
 
Ad

Recently uploaded (20)

PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PDF
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PDF
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
PDF
Digital water marking system project report
Kamal Acharya
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPTX
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
Digital water marking system project report
Kamal Acharya
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Ad

Extending php (7), the basics