Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Php Extensions for Dummies

17,387 views

Published on

Published in: Technology

Php Extensions for Dummies

  1. But I don’t know C!1. compiled2. strictly typed3. php internals do “hard” stuff4. copy and paste!5. cairo, pecl_http, date, imagick, dio lxr.php.net
  2. Quick Setup Needs1. compiler2. sdk (on win, put this on FIRST)3. tools4. dependencies5. code phpize is your friend!
  3. How to Compile1. phpize2. ./configure3. make4. make install5. make testconfigure might require –with-php-config
  4. How to Play alonghttps://github.com/auroraeosrose/php-extensions-code.gitgit://github.com/auroraeosrose/php-extensions-code.git
  5. Every Other Extensions Talk 1. Set up configuration 2. Write a module definition 3. Learn about the PHP lifecycle 4. Learn about zvals 5. Add functions 6. ??? 7. Profit!
  6. Which do YOU want?Please be the solution, not theproblemWe need no more painful APIsin PHP
  7. Step 1. What, When, Why 1. Do something you can’t do in userland 2. Utilize a C library 3. Make slow code faster Maybe you should just use ffi!
  8. FFI
  9. Step 2. How 1. Think about what the API should be 2. Look at the C APIs but don’t mimic them 3. Write an example of how you WANT it to work 4. Write more then one example!
  10. Step 3. Scaffolding 1. This is copy and paste 1. config.m4 & config.w32 2. macros for version api changes if necessary 3. main module file (php_{$ext}.c) 4. main header file (php_{$ext}.h) 2. Compile it and test!
  11. Configure files
  12. Module struct
  13. Scaffolding rules1. make sure you name your files in a standard way2. document! use a license header, proto statements3. read the coding standards http://lxr.php.net/xref/PHP_5_4/CODING_STANDARDS4. FOLLOW THE CODING STANDARDS5. use version control early on – github is easy!
  14. Check if it works php –d extension=myext.so –m The scaffold extension should show up in the list Make sure to 1. define a version constant 2. read and use PHP code standards
  15. Step 4. Write the test 1. run-test.php 2. make test will magically have output more at http://qa.php.net/write-test.php and docs at http://qa.php.net/phpt_details.php
  16. PHPT tests
  17. Step 5. Actually do something 1. add functions 2. get data 3. return data
  18. long any numeric double numeric with decimalchar* + int (length) strings, binaryHashtable dictionaries, arrays,structs object any complicated type
  19. Anatomy of a Function
  20. zval
  21. getting data OUTZ_LVAL(zval) Z_LVAL_P(zval_p) Z_LVAL_PP(zval_pp)Z_BVAL(zval) Z_BVAL_P(zval_p) Z_BVAL_PP(zval_pp)Z_DVAL(zval) Z_DVAL_P(zval_p) Z_DVAL_PP(zval_pp)Z_STRVAL(zval) Z_STRVAL_P(zval_p) Z_STRVAL_PP(zval_pp)Z_STRLEN(zval) Z_STRLEN_P(zval_p) Z_STRLEN_PP(zval_pp)Z_ARRVAL(zval) Z_ARRVAL_P(zval_p) Z_ARRVAL_PP(zval_pp)Z_OBJVAL(zval) Z_OBJVAL_P(zval_p) Z_OBJVAL_PP(zval_pp)Z_OBJ_HANDLE(zval) Z_OBJ_HANDLE_P(zval_p) Z_OBJ_HANDLE_PP(zval_pp)Z_OBJ_HT(zval) Z_OBJ_HT_P(zval_p) Z_OBJ_HT_PP(zval_pp)Z_OBJCE(zval) Z_OBJCE_P(zval_p) Z_OBJCE_PP(zval_pp)Z_OBJPROP(zval) Z_OBJPROP_P(zval_p) Z_OBJPROP_PP(zval_pp)Z_TYPE(zval) Z_TYPE_P(zval_p) Z_TYPE_PP(zval_pp)
  22. zend_parse_parameters codephp type c typearray or object a zval *boolean b zend_boolclass C zend_class_entry *double d doublecallable f zend_fcall_info andzend_fcall_info_cachearray or HASH_OF(object) H HashTable*array h HashTable*integer l long (NOT INT)integer L long with LONG_MAX, LONG_MIN limitsobject o zval *object of specific type O zval *, zend_class_entrystring (no null bytes) p char*, intresource r zval *string (possible null bytes) s char*, intactual zval z zval *actual zval Z zval**
  23. zend_parse_parameters codetypevariable args (any) * int, zval***variable args (1 or more) + int, zval*** | anything after is optional, use defaults / use SEPARATE_ZVAL_IF_NOT_REFdoesn’t apply to b, l, and d ! C NULL for zval null
  24. Returning Data with return_value RETURN_RESOURCE(l) RETVAL_RESOURCE(l) RETURN_BOOL(b) RETVAL_BOOL(b) RETURN_NULL() RETVAL_NULL() RETURN_LONG(l) RETVAL_LONG(l) RETURN_DOUBLE(d) RETVAL_DOUBLE(d) RETURN_STRING(s, duplicate) RETVAL_STRING(s, duplicate) RETURN_STRINGL(s, l, duplicate) RETVAL_STRINGL(s, l, duplicate) RETURN_EMPTY_STRING() RETVAL_EMPTY_STRING() RETURN_ZVAL(zv, copy, dtor) RETVAL_ZVAL(zv, copy, dtor) RETURN_FALSE RETURN_FALSE RETURN_TRUE RETURN_TRUE
  25. Complex Data array_init() object_init() add_(index|assoc)_long() add_property_long() add_(index|assoc)_bool() add_property_bool() add_property_string() add_(index|assoc)_string()
  26. Step 6. Make it shiny 1. namespaces 2. classes 3. methods 4. constants
  27. Classes 1. name, parent, and flags 2. hashtables of methods, default methods, static methods 3. hashtables of static properties, default properties, and properties 4. object handlers 5. union of either file information, or internal structures (for internal classes)
  28. Lifecycle PHP starts MINIT – for each extension RINIT – for each request GINIT – for each thread GSHUTDOWN – for each thread RSHUTDOWN – for each request MSHUTDOWN – for each extension PHP stops
  29. Class Properties
  30. Class Constants
  31. Class Methods
  32. Alter $this
  33. Abstract, Interface, TraitClass MethodZEND_ACC_IMPLICIT_ABSTRACT_CLASS ZEND_ACC_STATICZEND_ACC_EXPLICIT_ABSTRACT_CLASS ZEND_ACC_ABSTRACTZEND_ACC_FINAL_CLASS ZEND_ACC_FINALZEND_ACC_INTERFACE ZEND_ACC_PUBLICZEND_ACC_TRAIT ZEND_ACC_PROTECTED ZEND_ACC_PRIVATE ZEND_ACC_CTOR ZEND_ACC_DTOR ZEND_ACC_CLONEspl_ce_FilterIterator->ce_flags |=ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;PHP_ME(DateTime, __construct, arginfo_date_create,ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
  34. Step 7. Advanced topics 1. globals 2. memory management 3. custom objects 4. object handlers 5. thread safety
  35. Global Variables (threads = evil) 1. in your header – use ZEND_BEGIN|END_MODULE_GLOBALS 2. create the global access macro in your header (copy and paste) 3. ZEND_DECLARE_MODULE_GLOBALS in every file where you will use them 4. use the macro to access COUNTER_G(basic_counter_value)); } 5. Create ginit/gshutdown functions if your globals need initializing , etc
  36. emalloc( ) • allocates the specified number of bytessafe_emalloc() • like emalloc but adds a special protection against overflowsefree( ) • releases the specified block of memory back to the systemestrdup( ) • allocate a buffer and copy the string into that bufferestrndup( ) • same as estrdup when you already know the length of the stringecalloc( ) • allocates the number of bytes and initializes them to zeroerealloc( ) • resizes the specified block of memory https://wiki.php.net/internals/zend_mm
  37. 1. clean up what you emalloc (C level destructor)2. read wiki on how to make them extendable!
  38. Object Handlers (black magic) https://wiki.php.net/internals/engine/objects
  39. TSRM thread safe resource manager ZTS zend thread safetyTSRMLS_C • tsrm_lsTSRMLS_D • void ***tsrm_lsTSRMLS_CC • , tsrm_lsTSRMLS_DC • , void ***tsrm_ls
  40. Step 7. Document, PECL, release1. http://edit.php.net2. http://svn.php.net/viewvc/phpdoc/en/trunk/reference/3. PhD is awesomesauce • http://doc.php.net/phd/4. email pecl-dev@lists.php.net 1. who you are 2. what you wrote (with links to your code!) 3. why you think it should be in pecl5. poke me (or other devs)
  41. Stuff I didn’t talk about1. resources (use custom objects instead)2. ini entries (just DON’T)3. threading and parallel processing4. engine hooking
  42. About Me  http://emsmith.net  https://joind.in/6337  auroraeosrose@gmail.com  IRC – freenode – auroraeosrose  #php-gtk #coapp and others
  43. Questions? HELP WITH DOCS! http://edit.php.net http://wiki.php.net/internals
  44. Free codez 4 u!
  45. Resources:http://devzone.zend.com/303/extension-writing-part-i-introduction-to-php-and-zend/http://blog.golemon.com/2006/06/what-heck-is-tsrmlscc-anyway.htmlhttp://www.kchodorow.com/blog/2011/08/11/php-extensions-made-eldrich-installing-php/http://php.net/manual/en/internals2.phphttps://wiki.php.net/internalshttp://conf.phpquebec.com/slides/2009/PHP_Extension_Writing-phpquebec_2009.pdfhttp://devzone.zend.com/1435/wrapping-c-classes-in-a-php-extension/http://lxr.php.nethttp://www.amazon.com/Extending-Embedding-PHP-Sara-Golemon/dp/067232704X

×