Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP: Extending The Core - Why You Should Learn It

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.

Lucas Mendes

December 09, 2017
Tweet

More Decks by Lucas Mendes

Other Decks in Programming

Transcript

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

    ▸ Interpreted, imperative, procedural, object-oriented ▸ Dynamic typed ▸ Made for the web, used anywhere ▸ Extensible ▸ Powerful
  2. PHP: EXTENDING THE CORE EXTENSIONS ▸ Adds functionality ▸ Replaces

    functionality ▸ Change behavior ▸ Performance optimization ▸ Security improvement ▸ Link against an external library ▸ and many more…
  3. PHP: EXTENDING THE CORE PHP CORE EXTENSIONS ▸ ext/standard ▸

    ext/date ▸ ext/ereg ▸ ext/pcre ▸ ext/reflection ▸ ext/spl
  4. 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
  5. 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
  6. 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…
  7. 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"
  8. 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
  9. 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;
  10. 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;
  11. 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
  12. HANDLING VALUES zval *zv_ptr = /* ... */; if (Z_TYPE_P(zv_ptr)

    == IS_LONG) { php_printf("%ld\n”, Z_LVAL_P(zv_ptr)); } else /* ... */
  13. HASH TABLE STRUCTURE typedef struct _hashtable { uint32_t nTableSize; uint32_t

    nTableMask; uint32_t nNumOfElements; zend_long nNextFreeElement; ... } HashTable
  14. 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; }
  15. 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;
  16. PHP: EXTENDING THE CORE SYSTEM REQUIREMENTS ▸ gcc ▸ libc-dev

    ▸ make ▸ autoconf ▸ automake ▸ libtool ▸ bison (generates the parser) ▸ re2c (generates the lexer)
  17. THANK YOU! Lucas Mendes
 Software Architect at Tienda Nube
 about.me/devsdmf

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