Slide 1

Slide 1 text

Lucas Mendes | Software Architect | @devsdmf PHP: EXTENDING THE CORE WHY YOU SHOULD LEARN IT

Slide 2

Slide 2 text

$ whoami

Slide 3

Slide 3 text

PHP AT THE CORE UNDER THE HOOD…

Slide 4

Slide 4 text

OVERVIEW

Slide 5

Slide 5 text

PHP: EXTENDING THE CORE OVERVIEW ▸ Written in C Ansi ▸ Interpreted, imperative, procedural, object-oriented ▸ Dynamic typed ▸ Made for the web, used anywhere ▸ Extensible ▸ Powerful

Slide 6

Slide 6 text

ARCHITECTURE

Slide 7

Slide 7 text

PHP: EXTENDING THE CORE

Slide 8

Slide 8 text

EXTENSIONS

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

PHP: EXTENDING THE CORE EXTENSIONS ▸ Adds functionality ▸ Replaces functionality ▸ Change behavior ▸ Performance optimization ▸ Security improvement ▸ Link against an external library ▸ and many more…

Slide 11

Slide 11 text

CORE 
 EXTENSIONS

Slide 12

Slide 12 text

PHP: EXTENDING THE CORE PHP CORE EXTENSIONS ▸ ext/standard ▸ ext/date ▸ ext/ereg ▸ ext/pcre ▸ ext/reflection ▸ ext/spl

Slide 13

Slide 13 text

INTERNAL 
 EXTENSIONS

Slide 14

Slide 14 text

PHP: EXTENDING THE CORE PHP INTERNAL EXTENSIONS bcmath curl date gd ftp shmop iconv dom hash mysqli json intl pcntl mbstring soap xsl zlib Everything in ext/ folder

Slide 15

Slide 15 text

EXTERNAL 
 EXTENSIONS

Slide 16

Slide 16 text

PHP: EXTENDING THE CORE PECL: PHP EXTENSION COMMUNITY LIBRARY ▸ Too many extensions for various interesting (and odd things) ▸ Installed with PECL tool ▸ Successor of the Siberia ▸ pecl.php.net

Slide 17

Slide 17 text

PHP: EXTENDING THE CORE EXTENSIONS - EXTERNAL EXTENSIONS tk mongo imagick mcrypt binpack radius jsonc handlebars inotify solr apc libevent ev opengl tidy gender archive too many others…

Slide 18

Slide 18 text

PHP: EXTENDING THE CORE EXTENSIONS OVERVIEW

Slide 19

Slide 19 text

STATIC VS SHARED
 (EXTENSIONS)

Slide 20

Slide 20 text

PHP EXTENSIONS
 VS ZEND EXTENSIONS

Slide 21

Slide 21 text

EXTENSIONS 
 LIFETIME

Slide 22

Slide 22 text

EXTENSIONS LIFETIME

Slide 23

Slide 23 text

PHP REQUEST LIFECYCLES

Slide 24

Slide 24 text

PHP: EXTENDING THE CORE PHP REQUEST LIFECYCLE - BASICS

Slide 25

Slide 25 text

PHP: EXTENDING THE CORE PHP REQUEST LIFECYCLES - PROCESS-BASED MODEL

Slide 26

Slide 26 text

PHP: EXTENDING THE CORE PHP REQUEST LIFECYCLES - THREAD-BASED MODEL

Slide 27

Slide 27 text

PHP: EXTENDING THE CORE PHP REQUEST LIFECYCLES - EXECUTION

Slide 28

Slide 28 text

MEMORY MANAGEMENT

Slide 29

Slide 29 text

ZMM
 (ZEND MEMORY MANAGER)

Slide 30

Slide 30 text

REQUEST-BOUND VS PERMANENT (MEMORY ALLOCATION)

Slide 31

Slide 31 text

PHP: EXTENDING THE CORE REQUEST-BOUND MEMORY ALLOCATION ▸ Must only be performed when PHP is treating a request (not before, not after) ▸ Must use ZMM memory allocation API ▸ Most part of the extensions uses just request-bound memory management ▸ Are tracked by ZMM, you’ll be notified about leaking ▸ Respects the user land INI "memory_limit"

Slide 32

Slide 32 text

PHP: EXTENDING THE CORE PERMANENT MEMORY ALLOCATION ▸ Should not be performed while PHP is treating a request (not forbidden, but a bad idea) ▸ Uses standard libc memory allocation API (not ZMM) ▸ Should be pretty rare in an extension

Slide 33

Slide 33 text

THE INTERNALS LOOKING TO THE GEARS…

Slide 34

Slide 34 text

HOW MANY DATA TYPES DOES PHP HAVE ?

Slide 35

Slide 35 text

JUST ONE.

Slide 36

Slide 36 text

ZVAL typedef struct _zval_struct { zvalue_value value; union { struct { ZEND_ENDIAN_LOHI_4( zend_uchar type, zend_uchar type_flags, zend_uchar cosnt_flags, zend_uchar reserved) } v; uint32_t type_info; } u1; union {…} u2; } zval;

Slide 37

Slide 37 text

ZVALUE_VALUE typedef union _zvalue_value { zend_long lval; double dval; zend_refcounted *counted; zend_string *str; zend_array *arr; zend_resource *res; zend_object *obj; zend_class_entry *ce; zend_function *func; } zvalue_value;

Slide 38

Slide 38 text

VALUE TYPES Type Storage IS_NULL none IS_TRUE or IS_FALSE none IS_LONG zend_long lval IS_DOUBLE double dval IS_STRING zend_string *str IS_ARRAY zend_array *arr IS_OBJECT zend_object *obj IS_RESOURCE zend_resource *res

Slide 39

Slide 39 text

HANDLING VALUES zval *zv_ptr = /* ... */; if (Z_TYPE_P(zv_ptr) == IS_LONG) { php_printf("%ld\n”, Z_LVAL_P(zv_ptr)); } else /* ... */

Slide 40

Slide 40 text

HASH TABLES

Slide 41

Slide 41 text

HASH TABLE STRUCTURE typedef struct _hashtable { uint32_t nTableSize; uint32_t nTableMask; uint32_t nNumOfElements; zend_long nNextFreeElement; ... } HashTable

Slide 42

Slide 42 text

ZEND_ARRAY

Slide 43

Slide 43 text

OBJECTS

Slide 44

Slide 44 text

A SIMPLE CLASS zend_class_entry *ce_Example; zend_function_entry php_example_methods[] = { PHP_FE_END }; PHP_MINIT_FUNCTION(example) { zend_class_entry ce_example_local; INIT_CLASS_ENTRY(ce_example_local, “Example”, php_example_methods); ce_Example = zend_register_internal_class(&ce_example_local TSRMLS_CC); return SUCCESS; }

Slide 45

Slide 45 text

ZEND_OBJECT struct _zend_object { zend_refcounted_h gc; zend_class_entry *ce; const zend_object_handlers *handlers; HashTable *properties; zval properties_table[1]; } zend_object;

Slide 46

Slide 46 text

RESOURCES

Slide 47

Slide 47 text

ZEND_RESOURCE struct _zend_resource { zend_refcounted_h gc; int type; void *ptr; } zend_resource;

Slide 48

Slide 48 text

DEVELOPMENT ENVIRONMENT

Slide 49

Slide 49 text

REQUIREMENTS

Slide 50

Slide 50 text

PHP: EXTENDING THE CORE SYSTEM REQUIREMENTS ▸ gcc ▸ libc-dev ▸ make ▸ autoconf ▸ automake ▸ libtool ▸ bison (generates the parser) ▸ re2c (generates the lexer)

Slide 51

Slide 51 text

BUILDING THE PHP

Slide 52

Slide 52 text

DO NOT USE PREBUILT PACKAGES!

Slide 53

Slide 53 text

BUILD FROM SOURCE CODE.

Slide 54

Slide 54 text

EXTENSION DEVELOPMENT

Slide 55

Slide 55 text

EXTENSION SKELETON

Slide 56

Slide 56 text

PHP: EXTENDING THE CORE EXTENSION SKELETON ▸ config.m4 ▸ config.w32 ▸ hello.c ▸ php_hello.c ▸ tests/*

Slide 57

Slide 57 text

BUILD SYSTEM

Slide 58

Slide 58 text

PHP: EXTENDING THE CORE CONFIG.M4

Slide 59

Slide 59 text

PHP: EXTENDING THE CORE CONFIG.W32

Slide 60

Slide 60 text

REGISTERING THE EXTENSION

Slide 61

Slide 61 text

PHP: EXTENDING THE CORE REGISTERING THE EXTENSION

Slide 62

Slide 62 text

EXTENSION HOOKS

Slide 63

Slide 63 text

PHP: EXTENDING THE CORE MODULE INITIALIZATION

Slide 64

Slide 64 text

PHP: EXTENDING THE CORE MODULE SHUTDOWN

Slide 65

Slide 65 text

PHP: EXTENDING THE CORE REQUEST INITIALIZATION

Slide 66

Slide 66 text

PHP: EXTENDING THE CORE REQUEST SHUTDOWN

Slide 67

Slide 67 text

PHP: EXTENDING THE CORE MODULE INFORMATION

Slide 68

Slide 68 text

EXTENSION FUNCTIONS

Slide 69

Slide 69 text

PHP: EXTENDING THE CORE A SIMPLE FUNCTION

Slide 70

Slide 70 text

PHP: EXTENDING THE CORE HANDLING ARGUMENTS

Slide 71

Slide 71 text

INSTALLING

Slide 72

Slide 72 text

PHP: EXTENDING THE CORE PREPARING THE EXTENSION

Slide 73

Slide 73 text

PHP: EXTENDING THE CORE CONFIGURING THE BUILD

Slide 74

Slide 74 text

PHP: EXTENDING THE CORE INSTALLING

Slide 75

Slide 75 text

THANK YOU! Lucas Mendes
 Software Architect at Tienda Nube
 about.me/devsdmf We're hiring, join the crew! 
 bit.ly/work-at-tiendanube