PHP: Extending The Core - Why You Should Learn It

7a736764e6d6c830c049bdc2be34ec19?s=47 Lucas Mendes
December 09, 2017

PHP: Extending The Core - Why You Should Learn It

Have you ever thought about to be a contributor of the PHP core? Have you ever want to build a driver to access a hardware or even adding a new feature in the language? In this talk you will not only learn how to build your own PHP extension, but also change your way of programming, dissecting the language internals.

Presented at PHP Conference Brazil 2017.

7a736764e6d6c830c049bdc2be34ec19?s=128

Lucas Mendes

December 09, 2017
Tweet

Transcript

  1. Lucas Mendes | Software Architect | @devsdmf PHP: EXTENDING THE

    CORE WHY YOU SHOULD LEARN IT
  2. $ whoami

  3. PHP AT THE CORE UNDER THE HOOD…

  4. OVERVIEW

  5. PHP: EXTENDING THE CORE OVERVIEW ▸ Written in C Ansi

    ▸ Interpreted, imperative, procedural, object-oriented ▸ Dynamic typed ▸ Made for the web, used anywhere ▸ Extensible ▸ Powerful
  6. ARCHITECTURE

  7. PHP: EXTENDING THE CORE

  8. ZEND ENGINE

  9. EXTENSIONS

  10. None
  11. PHP: EXTENDING THE CORE EXTENSIONS ▸ Adds functionality ▸ Replaces

    functionality ▸ Change behavior ▸ Performance optimization ▸ Security improvement ▸ Link against an external library ▸ and many more…
  12. CORE 
 EXTENSIONS

  13. PHP: EXTENDING THE CORE PHP CORE EXTENSIONS ▸ ext/standard ▸

    ext/date ▸ ext/ereg ▸ ext/pcre ▸ ext/reflection ▸ ext/spl
  14. INTERNAL 
 EXTENSIONS

  15. 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
  16. EXTERNAL 
 EXTENSIONS

  17. 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
  18. 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…
  19. PHP: EXTENDING THE CORE EXTENSIONS OVERVIEW

  20. STATIC VS SHARED
 (EXTENSIONS)

  21. PHP EXTENSIONS
 VS ZEND EXTENSIONS

  22. EXTENSIONS 
 LIFETIME

  23. EXTENSIONS LIFETIME

  24. PHP REQUEST LIFECYCLES

  25. PHP: EXTENDING THE CORE PHP REQUEST LIFECYCLE - BASICS

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

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

  28. PHP: EXTENDING THE CORE PHP REQUEST LIFECYCLES - EXECUTION

  29. MEMORY MANAGEMENT

  30. ZMM
 (ZEND MEMORY MANAGER)

  31. REQUEST-BOUND VS PERMANENT (MEMORY ALLOCATION)

  32. 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"
  33. 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
  34. THE INTERNALS LOOKING TO THE GEARS…

  35. HOW MANY DATA TYPES DOES PHP HAVE ?

  36. JUST ONE.

  37. 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;
  38. 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;
  39. 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
  40. HANDLING VALUES zval *zv_ptr = /* ... */; if (Z_TYPE_P(zv_ptr)

    == IS_LONG) { php_printf("%ld\n”, Z_LVAL_P(zv_ptr)); } else /* ... */
  41. HASH TABLES

  42. HASH TABLE STRUCTURE typedef struct _hashtable { uint32_t nTableSize; uint32_t

    nTableMask; uint32_t nNumOfElements; zend_long nNextFreeElement; ... } HashTable
  43. ZEND_ARRAY

  44. OBJECTS

  45. 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; }
  46. 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;
  47. RESOURCES

  48. ZEND_RESOURCE struct _zend_resource { zend_refcounted_h gc; int type; void *ptr;

    } zend_resource;
  49. DEVELOPMENT ENVIRONMENT

  50. REQUIREMENTS

  51. PHP: EXTENDING THE CORE SYSTEM REQUIREMENTS ▸ gcc ▸ libc-dev

    ▸ make ▸ autoconf ▸ automake ▸ libtool ▸ bison (generates the parser) ▸ re2c (generates the lexer)
  52. BUILDING THE PHP

  53. DO NOT USE PREBUILT PACKAGES!

  54. BUILD FROM SOURCE CODE.

  55. EXTENSION DEVELOPMENT

  56. EXTENSION SKELETON

  57. PHP: EXTENDING THE CORE EXTENSION SKELETON ▸ config.m4 ▸ config.w32

    ▸ hello.c ▸ php_hello.c ▸ tests/*
  58. BUILD SYSTEM

  59. PHP: EXTENDING THE CORE CONFIG.M4

  60. PHP: EXTENDING THE CORE CONFIG.W32

  61. REGISTERING THE EXTENSION

  62. PHP: EXTENDING THE CORE REGISTERING THE EXTENSION

  63. EXTENSION HOOKS

  64. PHP: EXTENDING THE CORE MODULE INITIALIZATION

  65. PHP: EXTENDING THE CORE MODULE SHUTDOWN

  66. PHP: EXTENDING THE CORE REQUEST INITIALIZATION

  67. PHP: EXTENDING THE CORE REQUEST SHUTDOWN

  68. PHP: EXTENDING THE CORE MODULE INFORMATION

  69. EXTENSION FUNCTIONS

  70. PHP: EXTENDING THE CORE A SIMPLE FUNCTION

  71. PHP: EXTENDING THE CORE HANDLING ARGUMENTS

  72. INSTALLING

  73. PHP: EXTENDING THE CORE PREPARING THE EXTENSION

  74. PHP: EXTENDING THE CORE CONFIGURING THE BUILD

  75. PHP: EXTENDING THE CORE INSTALLING

  76. QUESTIONS ?

  77. THANK YOU! Lucas Mendes
 Software Architect at Tienda Nube
 about.me/devsdmf

    We're hiring, join the crew! 
 bit.ly/work-at-tiendanube