SlideShare a Scribd company logo
1 of 51
Download to read offline
But I don’t know C!
1. compiled
2. strictly typed
3. php internals do “hard” stuff
4. copy and paste!
5. cairo, pecl_http, date, imagick, dio

                           lxr.php.net
Quick Setup Needs
1. compiler
2. sdk (on win, put this on FIRST)
3. tools
4. dependencies
5. code
               phpize is your friend!
How to Compile
1. phpize
2. ./configure
3. make
4. make install
5. make test

configure might require –with-php-config
How to Play along
https://github.com/auroraeosrose/php-extensions-
code.git


git://github.com/auroraeosrose/php-extensions-
code.git
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!
Which do YOU
                                          want?




Please be the solution, not the
problem
We need no more painful APIs
in PHP
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!
FFI
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!
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!
Configure files
Module struct
Scaffolding rules
1. make sure you name your files in a standard way
2. document! use a license header, proto statements
3. read the coding standards
   http://lxr.php.net/xref/PHP_5_4/CODING_STANDARDS
4. FOLLOW THE CODING STANDARDS
5. use version control early on – github is easy!
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
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
PHPT tests
Step 5. Actually do something

 1. add functions
 2. get data
 3. return data
long              any numeric

 double         numeric with decimal

char* + int
 (length)           strings, binary

Hashtable     dictionaries, arrays,structs

  object        any complicated type
Anatomy of a Function
zval
getting data OUT
Z_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)
zend_parse_parameters          code
php type                              c type
array or object                a      zval *
boolean                        b      zend_bool
class                          C      zend_class_entry *
double                         d      double
callable                       f      zend_fcall_info andzend_fcall_info_cache
array or HASH_OF(object)       H      HashTable*
array                          h      HashTable*
integer                        l      long (NOT INT)
integer                        L      long with LONG_MAX, LONG_MIN limits
object                         o      zval *
object of specific type        O      zval *, zend_class_entry
string (no null bytes)         p      char*, int
resource                       r      zval *
string (possible null bytes)   s      char*, int
actual zval                    z      zval *
actual zval                    Z      zval**
zend_parse_parameters
                               code
type
variable args (any)            *      int, zval***

variable args (1 or more)      +      int, zval***

                               |      anything after is optional, use defaults
                               /      use SEPARATE_ZVAL_IF_NOT_REF
doesn’t apply to b, l, and d   !      C NULL for zval null
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
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()
Step 6. Make it shiny

 1. namespaces
 2. classes
 3. methods
 4. constants
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)
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
Class Properties
Class Constants
Class Methods
Alter $this
Abstract, Interface, Trait
Class                              Method
ZEND_ACC_IMPLICIT_ABSTRACT_CLASS   ZEND_ACC_STATIC
ZEND_ACC_EXPLICIT_ABSTRACT_CLASS ZEND_ACC_ABSTRACT
ZEND_ACC_FINAL_CLASS               ZEND_ACC_FINAL
ZEND_ACC_INTERFACE                 ZEND_ACC_PUBLIC
ZEND_ACC_TRAIT                     ZEND_ACC_PROTECTED
                                   ZEND_ACC_PRIVATE
                                   ZEND_ACC_CTOR
                                   ZEND_ACC_DTOR
                                   ZEND_ACC_CLONE

spl_ce_FilterIterator->ce_flags |=
ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
PHP_ME(DateTime, __construct, arginfo_date_create,
ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
Step 7. Advanced topics

 1. globals
 2. memory management
 3. custom objects
 4. object handlers
 5. thread safety
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
emalloc( )
 • allocates the specified number of bytes

safe_emalloc()
 • like emalloc but adds a special protection against overflows

efree( )
 • releases the specified block of memory back to the system

estrdup( )
 • allocate a buffer and copy the string into that buffer

estrndup( )
 • same as estrdup when you already know the length of the string

ecalloc( )
 • allocates the number of bytes and initializes them to zero

erealloc( )
 • resizes the specified block of memory
                  https://wiki.php.net/internals/zend_mm
1. clean up what you emalloc (C level destructor)
2. read wiki on how to make them extendable!
Object Handlers (black magic)




     https://wiki.php.net/internals/engine/objects
TSRM
   thread safe resource manager
                               ZTS
                 zend thread safety
TSRMLS_C    • tsrm_ls

TSRMLS_D    • void ***tsrm_ls

TSRMLS_CC   • , tsrm_ls

TSRMLS_DC   • , void ***tsrm_ls
Step 7. Document, PECL,
 release
1. http://edit.php.net
2. 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 pecl
5. poke me (or other devs)
Stuff I didn’t talk about

1. resources (use custom objects instead)
2. ini entries (just DON’T)
3. threading and parallel processing
4. engine hooking
About Me
      http://emsmith.net
      https://joind.in/6337
      auroraeosrose@gmail.com
      IRC – freenode – auroraeosrose
      #php-gtk #coapp and others
Questions?


     HELP WITH DOCS!
          http://edit.php.net
          http://wiki.php.net/internals
Free codez 4 u!
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.html
http://www.kchodorow.com/blog/2011/08/11/php-extensions-
made-eldrich-installing-php/
http://php.net/manual/en/internals2.php
https://wiki.php.net/internals
http://conf.phpquebec.com/slides/2009/PHP_Extension_Writing
-phpquebec_2009.pdf
http://devzone.zend.com/1435/wrapping-c-classes-in-a-php-
extension/
http://lxr.php.net
http://www.amazon.com/Extending-Embedding-PHP-Sara-
Golemon/dp/067232704X

More Related Content

What's hot

Yapc::NA::2009 - Command Line Perl
Yapc::NA::2009 - Command Line PerlYapc::NA::2009 - Command Line Perl
Yapc::NA::2009 - Command Line PerlBruce Gray
 
OpenGurukul : Language : PHP
OpenGurukul : Language : PHPOpenGurukul : Language : PHP
OpenGurukul : Language : PHPOpen Gurukul
 
Perl Tidy Perl Critic
Perl Tidy Perl CriticPerl Tidy Perl Critic
Perl Tidy Perl Criticolegmmiller
 
OpenGurukul : Language : Python
OpenGurukul : Language : PythonOpenGurukul : Language : Python
OpenGurukul : Language : PythonOpen Gurukul
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Pythonkwatch
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation TutorialLorna Mitchell
 
Programming with Python - Adv.
Programming with Python - Adv.Programming with Python - Adv.
Programming with Python - Adv.Mosky Liu
 
P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)lichtkind
 
Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8Nikita Popov
 
Secure Programming Practices in C++ (NDC Security 2018)
Secure Programming Practices in C++ (NDC Security 2018)Secure Programming Practices in C++ (NDC Security 2018)
Secure Programming Practices in C++ (NDC Security 2018)Patricia Aas
 
Perl 5.10
Perl 5.10Perl 5.10
Perl 5.10acme
 
Handling inline assembly in Clang and LLVM
Handling inline assembly in Clang and LLVMHandling inline assembly in Clang and LLVM
Handling inline assembly in Clang and LLVMMin-Yih Hsu
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 
Interceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalInterceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalKent Ohashi
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Fwdays
 

What's hot (20)

Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Yapc::NA::2009 - Command Line Perl
Yapc::NA::2009 - Command Line PerlYapc::NA::2009 - Command Line Perl
Yapc::NA::2009 - Command Line Perl
 
Perl Moderno
Perl ModernoPerl Moderno
Perl Moderno
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Java Full Throttle
Java Full ThrottleJava Full Throttle
Java Full Throttle
 
OpenGurukul : Language : PHP
OpenGurukul : Language : PHPOpenGurukul : Language : PHP
OpenGurukul : Language : PHP
 
Os Goodger
Os GoodgerOs Goodger
Os Goodger
 
Perl Tidy Perl Critic
Perl Tidy Perl CriticPerl Tidy Perl Critic
Perl Tidy Perl Critic
 
OpenGurukul : Language : Python
OpenGurukul : Language : PythonOpenGurukul : Language : Python
OpenGurukul : Language : Python
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Python
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
Programming with Python - Adv.
Programming with Python - Adv.Programming with Python - Adv.
Programming with Python - Adv.
 
P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)
 
Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8
 
Secure Programming Practices in C++ (NDC Security 2018)
Secure Programming Practices in C++ (NDC Security 2018)Secure Programming Practices in C++ (NDC Security 2018)
Secure Programming Practices in C++ (NDC Security 2018)
 
Perl 5.10
Perl 5.10Perl 5.10
Perl 5.10
 
Handling inline assembly in Clang and LLVM
Handling inline assembly in Clang and LLVMHandling inline assembly in Clang and LLVM
Handling inline assembly in Clang and LLVM
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Interceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalInterceptors: Into the Core of Pedestal
Interceptors: Into the Core of Pedestal
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
 

Viewers also liked

Multi-tasking in PHP
Multi-tasking in PHPMulti-tasking in PHP
Multi-tasking in PHPJonathon Hill
 
La niña y el duende
La niña y el duendeLa niña y el duende
La niña y el duendejhonpapito
 
192000470 sach-tự-luyện-toeic-900-a-b
192000470 sach-tự-luyện-toeic-900-a-b192000470 sach-tự-luyện-toeic-900-a-b
192000470 sach-tự-luyện-toeic-900-a-bThân Lan Hương
 
Rich Internet Applications mit SharePoint
Rich Internet Applications mit SharePointRich Internet Applications mit SharePoint
Rich Internet Applications mit SharePointbusitec GmbH
 
Das Recruiting der Zukunft - aufwändig, menschlich und erfolgreich!
Das Recruiting der Zukunft - aufwändig, menschlich und erfolgreich!Das Recruiting der Zukunft - aufwändig, menschlich und erfolgreich!
Das Recruiting der Zukunft - aufwändig, menschlich und erfolgreich!Henrik Zaborowski
 
The Content Marketing Manifesto
The Content Marketing ManifestoThe Content Marketing Manifesto
The Content Marketing ManifestoRand Fishkin
 
Presentación medios de pago
Presentación medios de pagoPresentación medios de pago
Presentación medios de pagoBryan Morales
 
Texting marketing advantages
Texting marketing advantagesTexting marketing advantages
Texting marketing advantagesLeo Vidal
 
Phpcompilerinternals 090824022750-phpapp02
Phpcompilerinternals 090824022750-phpapp02Phpcompilerinternals 090824022750-phpapp02
Phpcompilerinternals 090824022750-phpapp02philipo
 
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure National Cheng Kung University
 
Jaaj 54 fev13
Jaaj 54 fev13Jaaj 54 fev13
Jaaj 54 fev13aplalmir
 
El principiodeigualdadenlanegociacioncolectiva ccncc
El principiodeigualdadenlanegociacioncolectiva ccnccEl principiodeigualdadenlanegociacioncolectiva ccncc
El principiodeigualdadenlanegociacioncolectiva ccnccComunidad Beksa
 

Viewers also liked (20)

Multi-tasking in PHP
Multi-tasking in PHPMulti-tasking in PHP
Multi-tasking in PHP
 
Seguiremos, cantamos por la paz 2015
Seguiremos, cantamos por la paz 2015Seguiremos, cantamos por la paz 2015
Seguiremos, cantamos por la paz 2015
 
Curso inglés C1
Curso inglés C1Curso inglés C1
Curso inglés C1
 
La niña y el duende
La niña y el duendeLa niña y el duende
La niña y el duende
 
Prima catalogue
Prima cataloguePrima catalogue
Prima catalogue
 
192000470 sach-tự-luyện-toeic-900-a-b
192000470 sach-tự-luyện-toeic-900-a-b192000470 sach-tự-luyện-toeic-900-a-b
192000470 sach-tự-luyện-toeic-900-a-b
 
Rich Internet Applications mit SharePoint
Rich Internet Applications mit SharePointRich Internet Applications mit SharePoint
Rich Internet Applications mit SharePoint
 
PHP 7 new engine
PHP 7 new enginePHP 7 new engine
PHP 7 new engine
 
Das Recruiting der Zukunft - aufwändig, menschlich und erfolgreich!
Das Recruiting der Zukunft - aufwändig, menschlich und erfolgreich!Das Recruiting der Zukunft - aufwändig, menschlich und erfolgreich!
Das Recruiting der Zukunft - aufwändig, menschlich und erfolgreich!
 
The Content Marketing Manifesto
The Content Marketing ManifestoThe Content Marketing Manifesto
The Content Marketing Manifesto
 
Presentación medios de pago
Presentación medios de pagoPresentación medios de pago
Presentación medios de pago
 
Texting marketing advantages
Texting marketing advantagesTexting marketing advantages
Texting marketing advantages
 
Kgosni 85
Kgosni 85Kgosni 85
Kgosni 85
 
Phpcompilerinternals 090824022750-phpapp02
Phpcompilerinternals 090824022750-phpapp02Phpcompilerinternals 090824022750-phpapp02
Phpcompilerinternals 090824022750-phpapp02
 
How PHP works
How PHP works How PHP works
How PHP works
 
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
Accelerating or Complicating PHP execution by LLVM Compiler Infrastructure
 
PHP Internals
PHP InternalsPHP Internals
PHP Internals
 
Jaaj 54 fev13
Jaaj 54 fev13Jaaj 54 fev13
Jaaj 54 fev13
 
El principiodeigualdadenlanegociacioncolectiva ccncc
El principiodeigualdadenlanegociacioncolectiva ccnccEl principiodeigualdadenlanegociacioncolectiva ccncc
El principiodeigualdadenlanegociacioncolectiva ccncc
 
Build Programming Language Runtime with LLVM
Build Programming Language Runtime with LLVMBuild Programming Language Runtime with LLVM
Build Programming Language Runtime with LLVM
 

Similar to Php Extensions for Dummies

Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy CodeRowan Merewood
 
Extending php (7), the basics
Extending php (7), the basicsExtending php (7), the basics
Extending php (7), the basicsPierre Joye
 
The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014Matthias Noback
 
Using spl tools in your code
Using spl tools in your codeUsing spl tools in your code
Using spl tools in your codeElizabeth Smith
 
The Naked Bundle - Symfony Usergroup Belgium
The Naked Bundle - Symfony Usergroup BelgiumThe Naked Bundle - Symfony Usergroup Belgium
The Naked Bundle - Symfony Usergroup BelgiumMatthias Noback
 
The Naked Bundle - Symfony Barcelona
The Naked Bundle - Symfony BarcelonaThe Naked Bundle - Symfony Barcelona
The Naked Bundle - Symfony BarcelonaMatthias Noback
 
Php extensions workshop
Php extensions workshopPhp extensions workshop
Php extensions workshopjulien pauli
 
TYPO3 Extension development using new Extbase framework
TYPO3 Extension development using new Extbase frameworkTYPO3 Extension development using new Extbase framework
TYPO3 Extension development using new Extbase frameworkChristian Trabold
 
Php7 extensions workshop
Php7 extensions workshopPhp7 extensions workshop
Php7 extensions workshopjulien pauli
 
Ts archiving
Ts   archivingTs   archiving
Ts archivingConfiz
 
Unit 4
Unit 4Unit 4
Unit 4siddr
 
Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Elizabeth Smith
 

Similar to Php Extensions for Dummies (20)

Php extensions
Php extensionsPhp extensions
Php extensions
 
Php extensions
Php extensionsPhp extensions
Php extensions
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Extending php (7), the basics
Extending php (7), the basicsExtending php (7), the basics
Extending php (7), the basics
 
The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014The Naked Bundle - Symfony Live London 2014
The Naked Bundle - Symfony Live London 2014
 
Using spl tools in your code
Using spl tools in your codeUsing spl tools in your code
Using spl tools in your code
 
Gcrc talk
Gcrc talkGcrc talk
Gcrc talk
 
The Naked Bundle - Symfony Usergroup Belgium
The Naked Bundle - Symfony Usergroup BelgiumThe Naked Bundle - Symfony Usergroup Belgium
The Naked Bundle - Symfony Usergroup Belgium
 
Advance python
Advance pythonAdvance python
Advance python
 
The Naked Bundle - Symfony Barcelona
The Naked Bundle - Symfony BarcelonaThe Naked Bundle - Symfony Barcelona
The Naked Bundle - Symfony Barcelona
 
Php extensions workshop
Php extensions workshopPhp extensions workshop
Php extensions workshop
 
TYPO3 Extension development using new Extbase framework
TYPO3 Extension development using new Extbase frameworkTYPO3 Extension development using new Extbase framework
TYPO3 Extension development using new Extbase framework
 
Spl in the wild
Spl in the wildSpl in the wild
Spl in the wild
 
Php7 extensions workshop
Php7 extensions workshopPhp7 extensions workshop
Php7 extensions workshop
 
обзор Python
обзор Pythonобзор Python
обзор Python
 
Writing MySQL UDFs
Writing MySQL UDFsWriting MySQL UDFs
Writing MySQL UDFs
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
Ts archiving
Ts   archivingTs   archiving
Ts archiving
 
Unit 4
Unit 4Unit 4
Unit 4
 
Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09Spl to the Rescue - Zendcon 09
Spl to the Rescue - Zendcon 09
 

More from Elizabeth Smith

Database theory and modeling
Database theory and modelingDatabase theory and modeling
Database theory and modelingElizabeth Smith
 
Taming the resource tiger
Taming the resource tigerTaming the resource tiger
Taming the resource tigerElizabeth Smith
 
Taming the resource tiger
Taming the resource tigerTaming the resource tiger
Taming the resource tigerElizabeth Smith
 
Php internal architecture
Php internal architecturePhp internal architecture
Php internal architectureElizabeth Smith
 
Taming the tiger - pnwphp
Taming the tiger - pnwphpTaming the tiger - pnwphp
Taming the tiger - pnwphpElizabeth Smith
 
Security is not a feature
Security is not a featureSecurity is not a feature
Security is not a featureElizabeth Smith
 
Mentoring developers-php benelux-2014
Mentoring developers-php benelux-2014Mentoring developers-php benelux-2014
Mentoring developers-php benelux-2014Elizabeth Smith
 
Socket programming with php
Socket programming with phpSocket programming with php
Socket programming with phpElizabeth Smith
 
Spl in the wild - zendcon2012
Spl in the wild - zendcon2012Spl in the wild - zendcon2012
Spl in the wild - zendcon2012Elizabeth Smith
 
Mentoring developers - Zendcon 2012
Mentoring developers - Zendcon 2012Mentoring developers - Zendcon 2012
Mentoring developers - Zendcon 2012Elizabeth Smith
 
Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012Elizabeth Smith
 

More from Elizabeth Smith (20)

Welcome to the internet
Welcome to the internetWelcome to the internet
Welcome to the internet
 
Database theory and modeling
Database theory and modelingDatabase theory and modeling
Database theory and modeling
 
Taming the resource tiger
Taming the resource tigerTaming the resource tiger
Taming the resource tiger
 
Modern sql
Modern sqlModern sql
Modern sql
 
Taming the resource tiger
Taming the resource tigerTaming the resource tiger
Taming the resource tiger
 
Php internal architecture
Php internal architecturePhp internal architecture
Php internal architecture
 
Taming the tiger - pnwphp
Taming the tiger - pnwphpTaming the tiger - pnwphp
Taming the tiger - pnwphp
 
Php’s guts
Php’s gutsPhp’s guts
Php’s guts
 
Lexing and parsing
Lexing and parsingLexing and parsing
Lexing and parsing
 
Hacking with hhvm
Hacking with hhvmHacking with hhvm
Hacking with hhvm
 
Security is not a feature
Security is not a featureSecurity is not a feature
Security is not a feature
 
Using unicode with php
Using unicode with phpUsing unicode with php
Using unicode with php
 
Mentoring developers-php benelux-2014
Mentoring developers-php benelux-2014Mentoring developers-php benelux-2014
Mentoring developers-php benelux-2014
 
Using unicode with php
Using unicode with phpUsing unicode with php
Using unicode with php
 
Socket programming with php
Socket programming with phpSocket programming with php
Socket programming with php
 
Mentoring developers
Mentoring developersMentoring developers
Mentoring developers
 
Do the mentor thing
Do the mentor thingDo the mentor thing
Do the mentor thing
 
Spl in the wild - zendcon2012
Spl in the wild - zendcon2012Spl in the wild - zendcon2012
Spl in the wild - zendcon2012
 
Mentoring developers - Zendcon 2012
Mentoring developers - Zendcon 2012Mentoring developers - Zendcon 2012
Mentoring developers - Zendcon 2012
 
Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012
 

Recently uploaded

UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessWSO2
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Nikki Chapple
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfAarwolf Industries LLC
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...amber724300
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialJoão Esperancinha
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 

Recently uploaded (20)

UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with Platformless
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdf
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorial
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 

Php Extensions for Dummies

  • 1.
  • 2. But I don’t know C! 1. compiled 2. strictly typed 3. php internals do “hard” stuff 4. copy and paste! 5. cairo, pecl_http, date, imagick, dio lxr.php.net
  • 3. Quick Setup Needs 1. compiler 2. sdk (on win, put this on FIRST) 3. tools 4. dependencies 5. code phpize is your friend!
  • 4. How to Compile 1. phpize 2. ./configure 3. make 4. make install 5. make test configure might require –with-php-config
  • 5. How to Play along https://github.com/auroraeosrose/php-extensions- code.git git://github.com/auroraeosrose/php-extensions- code.git
  • 6. 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!
  • 7.
  • 8. Which do YOU want? Please be the solution, not the problem We need no more painful APIs in PHP
  • 9. 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!
  • 10. FFI
  • 11. 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!
  • 12.
  • 13. 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!
  • 16. Scaffolding rules 1. make sure you name your files in a standard way 2. document! use a license header, proto statements 3. read the coding standards http://lxr.php.net/xref/PHP_5_4/CODING_STANDARDS 4. FOLLOW THE CODING STANDARDS 5. use version control early on – github is easy!
  • 17. 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
  • 18. 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
  • 20. Step 5. Actually do something 1. add functions 2. get data 3. return data
  • 21. long any numeric double numeric with decimal char* + int (length) strings, binary Hashtable dictionaries, arrays,structs object any complicated type
  • 22. Anatomy of a Function
  • 23. zval
  • 24. getting data OUT Z_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)
  • 25. zend_parse_parameters code php type c type array or object a zval * boolean b zend_bool class C zend_class_entry * double d double callable f zend_fcall_info andzend_fcall_info_cache array or HASH_OF(object) H HashTable* array h HashTable* integer l long (NOT INT) integer L long with LONG_MAX, LONG_MIN limits object o zval * object of specific type O zval *, zend_class_entry string (no null bytes) p char*, int resource r zval * string (possible null bytes) s char*, int actual zval z zval * actual zval Z zval**
  • 26. zend_parse_parameters code type variable args (any) * int, zval*** variable args (1 or more) + int, zval*** | anything after is optional, use defaults / use SEPARATE_ZVAL_IF_NOT_REF doesn’t apply to b, l, and d ! C NULL for zval null
  • 27. 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
  • 28. 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()
  • 29. Step 6. Make it shiny 1. namespaces 2. classes 3. methods 4. constants
  • 30. 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)
  • 31.
  • 32.
  • 33. 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
  • 38. Abstract, Interface, Trait Class Method ZEND_ACC_IMPLICIT_ABSTRACT_CLASS ZEND_ACC_STATIC ZEND_ACC_EXPLICIT_ABSTRACT_CLASS ZEND_ACC_ABSTRACT ZEND_ACC_FINAL_CLASS ZEND_ACC_FINAL ZEND_ACC_INTERFACE ZEND_ACC_PUBLIC ZEND_ACC_TRAIT ZEND_ACC_PROTECTED ZEND_ACC_PRIVATE ZEND_ACC_CTOR ZEND_ACC_DTOR ZEND_ACC_CLONE spl_ce_FilterIterator->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS; PHP_ME(DateTime, __construct, arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
  • 39. Step 7. Advanced topics 1. globals 2. memory management 3. custom objects 4. object handlers 5. thread safety
  • 40. 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
  • 41. emalloc( ) • allocates the specified number of bytes safe_emalloc() • like emalloc but adds a special protection against overflows efree( ) • releases the specified block of memory back to the system estrdup( ) • allocate a buffer and copy the string into that buffer estrndup( ) • same as estrdup when you already know the length of the string ecalloc( ) • allocates the number of bytes and initializes them to zero erealloc( ) • resizes the specified block of memory https://wiki.php.net/internals/zend_mm
  • 42.
  • 43. 1. clean up what you emalloc (C level destructor) 2. read wiki on how to make them extendable!
  • 44. Object Handlers (black magic) https://wiki.php.net/internals/engine/objects
  • 45. TSRM thread safe resource manager ZTS zend thread safety TSRMLS_C • tsrm_ls TSRMLS_D • void ***tsrm_ls TSRMLS_CC • , tsrm_ls TSRMLS_DC • , void ***tsrm_ls
  • 46. Step 7. Document, PECL, release 1. http://edit.php.net 2. 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 pecl 5. poke me (or other devs)
  • 47. Stuff I didn’t talk about 1. resources (use custom objects instead) 2. ini entries (just DON’T) 3. threading and parallel processing 4. engine hooking
  • 48. About Me  http://emsmith.net  https://joind.in/6337  auroraeosrose@gmail.com  IRC – freenode – auroraeosrose  #php-gtk #coapp and others
  • 49. Questions? HELP WITH DOCS! http://edit.php.net http://wiki.php.net/internals

Editor's Notes

  1. I mentor PHP developers into becoming C developers – because other PHP devs have done that for meIf you want to move on and do more with extensions after this talk please talk to me – I also sit on freenode all day and answer questions and give feedback – always always want more code monkeys and fresh blood in any projectand looking at github extensions makes me sad – 99% suck
  2. This is the #1 argument I hear – but really is not anything you need to worry aboutc98 people! you can use some c99 stuff but declarations MUST go at the top of blocsk (that’s really good coding standards anyway)just like phpiself turn all your errors on when compiling (-wall is your friend) and try to code cleanyou don’t have to “know C” anymore then you have to “know PHP” to write a wordpress pluginif you have the very basics of what to do it’s not hardPHP takes care of a lot of the heavy lifting – from how to parse parameters coming in and how to shove data into someplace going out…to how to do fancy objectsUnless you’re doing something REALLY evil (an opcode cache, changing the way the engine works, threading) most of this stuff has already been done for another extension, it’s just a matter of finding the code – compile and testbtw, all the code generators out there currently suck – most don’t do test generation, doc generation, or use the proper apis
  3. This is the same and yet different on every system and we’re not going to go into it a whole lot – this should have been homework before you came ;)Basically you need a compiler – xcode on mac, vc(2008) express on windows, gcc something on linux – this is different for every systemYou also need the sdk and headers for your system – those will be put on with xcode, you’ll need the 6.1 windows sdk for windowsThen you need autotools – bison, and re2c to build PHPOn mac or linux you can use phpize to build extensions – You CAN do this on windows as well except that for some stupid reason the proper files are not shipped with windows binary builds (stupid)Then you need any depedencies (to build PHP as a base you need iconv, zlib, and libxml2 at a bare minimum)After you build PHP on windows you can use phpize (it will be generated for you) and makes life so much easier
  4. make a note that windows does not need the ./ before configure and uses nmake instead of make (otherwise identical)Note that to use a specific PHP install use /full/path/to/phpize and –with-php-config=/usr/local/php5/bin/php-configNote this does shared extensions only – then again most of the extensions you’ll work on should probably be shared
  5. I’m putting parts of this code onto my github account – each section I talk about – scaffolding, adding a function, ext, will have a different branch – feel free to clone and playThese aren’t necessarily complicated extensions but they do follow PHP CS, have all the copy and paste you need and do compile (I think)
  6. If you’ve ever seen another PHP extensions talk they dive into how to use the zend engine and how things work and suchPHP is to C as wordpress is to PHP – makes any idiot able to write a plugin for it ;)you don’t necessarily HAVE to know how the internals work – you just have to know the right calls to make!So although I might tell you about SOME of the internals how and why, for the most part this will be “how to do it” not “why you do it”Because PHP is a ball of rusty nails ;)
  7. In addition this is the absolutely wrong way to approach writing PHP extensions!Actually it’s the wrong way to approach writing LOTS of thingsdo you start out writing a website worrying about how to make a db connection before you’ve designed the database and created the schema? do you create the schema before you have wireframes? just because we’re making a different product doesn’t mean the rules change
  8. this is why you really need to think about what you’re doing – being able to use the extension is not really enough… just wrapping a C api is a good way to give yourselves headaches of a horrible nature
  9. so these are the reasons you’d write a PHP extension – you want to use a C library, you want to make something process intensive faster, you want to hook into the engine itselfthere aren’t other reasons to do an extension
  10. before you get into extension writing – if you just want an ffi wrapper and are just going to call the exact C calls from an existing library why go to the trouble of writing an extension?ffi is pretty great but a bit of a flakey extension yet, but it’s identical to python’s “ctypes” which is a stupid name, it’s really ffiI hear al lthe time about how “great” python is because of ctypes, frankly I beg to differ. Part of wrappign a C extension is translating the C calls into something far more “phpish”
  11. C++ APIs or similar wrappers in python, perl, ruby, etc are a better place to look for api ideas – try a couple of ways of using the apis as wellreally think about what kinds of apis and code you’re looking for – so I was looking at wrapping a small library (uriparser.sourceforge.net) and one of the things I did first was sit down and write some rough almost pseudo-code of what the extension might look like
  12. uriparser.sourceforge.net – a simple uri parser library that strictly conforms to rfchow would thibgs work?do I want namespaces? will this be an object oriented api or a functional api? do I want to do the extra work of a dual api? (such as date or mysqli?) these decisions are usually pretty easy to figure out once you actually try to write the kind of code you want to work
  13. PHP is 90% copy and paste, the main copy and paste stuff is all scaffolding for an extensionso we’ve figure out what we want to write and how we want to write it, now we need to have scaffolding in place for ittalk about our 4 files, the scaffold example - show the files quickly to show what normally goes in a header, some PHP CS rules, et al
  14. DO BOTH unless you’re absolutely certain that whatever you’re writing will only work on one system or the otherm4 is autotools stuff, but the w32 are very easy to write as well, they’re just jscript (windows javascript dialect) with some special functionsfor things like checking headers and adding libs, etc
  15. this is the very basic items you can have in your struct to define what you module is and what it doesSTANDARD_MODULE_HEADER_EX allows you to add ini entries and depedencies to your module definitionusing STANDARD_MODULE_PROPERTIES_EX allows you to add globalswhat the globals are, a ginit and a gshutdown – plus a “post deactivate” method that basically never gets used ;)
  16. Just some general rules for you to take a look ateven if your extension is not going into PHP proper the more time you take to properly document your code the happier you will be
  17. how to see if what you’ve been playing with will actually workThis should load your boring extension without doing anything fancy, just allow it to show up in the modules listyou can actually do a test case for this with extension_loaded or get_loaded_modules if you’re so inclined
  18. everybody should ALWAYS test their extensions – test testtestturn debugging on, turn memory leaks on - run your testsif they fail you’ll get a bunch of extra files from the tests system available that make things very easy to debug, a diff file, .out file, even an .shfile to run the test exactly like it was run the first timeno .bat file yet… yes I have a patch for that
  19. These are very basic functional style teststhere are a bunch of different sections for each one – the important thing to remember is always include a skipifanything in “tests” will be run so it’s ok to put things in directories for orderwrite tests for everything you can think of! this is my standard test to check my phpinfo (MINFO) function
  20. So we’re going to actually write a function for our uriparser library that does something extremely simple – we’re going to register a function to tell us the uriparser library version
  21. So basically with an extension (especially one wrapping a library) what you’re doingis taking C variables and exposing them to php land by turning them into zvalsSo start by thinking about what PHP offers as variable typeslong lval; /* long value */309 double dval; /* double value */310 struct {311 char *val;312 intlen;313 } str;314 HashTable *ht; /* hash table value */zend_object_valueobj;so a “zval” in PHP has a union that can have the value of what is inside – something you’ll need to do when you create your extension is figure out how to “translate” that to a PHP type and how to handle it
  22. you need to do three things to add a function to your extensiondefine it’s argumentsdefine it’s bodyput it inside a function structarginfo must have the begin and end arginfothen you add additional arguments as desired in betweenif you have optional arguments, use begin_arginfo_exyou can send by val or reference or do typehinting with arginfo as well
  23. Our zval is a giant struct of doomfor those of you who don’t know what a union is, A union, is a collection of variables of different types, just like a structure. However, with unions, you can only store information in one field at any one time. You can picture a union as like a chunk of memory that is used to store variables of different types. Once a new value is assigned to a field, the existing data is wiped over with the new data. This is what everything you do in C goes into!
  24. p is for pointer (that’s good enough for me)these are helper macros to get the data OUT of that union of doom that you need, it’s also how you get zval types and other fun stuffnote there are even MORE then this, but these are the ones you’ll use most oftenPHP is governed by macros and more macros, which is why it’s important to look at existing PHP extensions since 95% of the macros are NOT documented
  25. so you can also do “zend_parse_parameters_ex” the only thing you can do it with is QUIETthere are a couple of reasons for doing this – mostly nesting and overloaded calls
  26. This is macro hell – PHP is macro hellevery PHP function (or method) has a return value available to you – an automatically NULL zval that you need ot manipulate in order to give information backit’s called return_valueyou manipulate these return value zvals to get data back into PHP userland
  27. When you start to want to return complex data things get a little more complicated – this is where array and object init come inthere are also ways to array_init with a specific length or object_init_ex which allows you to choose the type of object you’re creatingthere are a bunch more of thesezend_API.h is your lifeblood for how to use these things, along with
  28. Now we move on to more complex parts of the code, methods that really DO somethingremember we should start by writing tests for it
  29. the struct for a zend_class_entry is SOOOO big I can’t show it on one slide ;)however it’s almost entirely pointers to other stuffstruct _zend_class_entry {463 char type;464 const char *name;465 zend_uintname_length;466 struct _zend_class_entry *parent;467 intrefcount;468 zend_uintce_flags;469470 HashTablefunction_table;471 HashTableproperties_info;472 zval **default_properties_table;473 zval **default_static_members_table;474 zval **static_members_table;475 HashTableconstants_table;476 intdefault_properties_count;477 intdefault_static_members_count;478479 union _zend_function *constructor;480 union _zend_function *destructor;481 union _zend_function *clone;482 union _zend_function *__get;483 union _zend_function *__set;484 union _zend_function *__unset;485 union _zend_function *__isset;486 union _zend_function *__call;487 union _zend_function *__callstatic;488 union _zend_function *__tostring;489 union _zend_function *serialize_func;490 union _zend_function *unserialize_func;491492 zend_class_iterator_funcsiterator_funcs;493494 /* handlers */495 zend_object_value (*create_object)(zend_class_entry *class_type TSRMLS_DC);496 zend_object_iterator *(*get_iterator)(zend_class_entry *ce, zval *object, intby_ref TSRMLS_DC);497 int (*interface_gets_implemented)(zend_class_entry *iface, zend_class_entry *class_type TSRMLS_DC); /* a class implements this interface */498 union _zend_function *(*get_static_method)(zend_class_entry *ce, char* method, intmethod_len TSRMLS_DC);499500 /* serializer callbacks */501 int (*serialize)(zval *object, unsigned char **buffer, zend_uint *buf_len, zend_serialize_data *data TSRMLS_DC);502 int (*unserialize)(zval **object, zend_class_entry *ce, const unsigned char *buf, zend_uintbuf_len, zend_unserialize_data *data TSRMLS_DC);503504 zend_class_entry **interfaces;505 zend_uintnum_interfaces;506507 zend_class_entry **traits;508 zend_uintnum_traits;509 zend_trait_alias **trait_aliases;510 zend_trait_precedence **trait_precedences;511512 union {513 struct {514 const char *filename;515 zend_uintline_start;516 zend_uintline_end;517 const char *doc_comment;518 zend_uintdoc_comment_len;519 } user;520 struct {521 conststruct _zend_function_entry *builtin_functions;522 struct _zend_module_entry *module;523 } internal;524 } info;525};
  30. TO do a class – create and register it’s CE in minitput it in another file – will keep yourself sane instead of having 300 mile long filessome people whine that this makes it harder to use static stuff, since each file is compiled into it’s own “unit” and then linkedbut modern compilers and linkers make the overhead on this moot – better to be organized!
  31. so this is how you would do your minit function – this would go into php_uriparser.cwhere the other code displayed would be in uri.c – where all our code for our uri class is goingit is generally a good idea to keep arginfo and methods to gether as well in the code, so when one changes you remember to change the otherremember stuff must be declared in order to be used too!the namespace declaration and the PHP_MINIT declaration go into the header fileI often do a private header file for things like that – or do an api file if I allow things in my extension to be used by say other extensions
  32. so this is the lifecycle of PHPeach of these items has a different important role to playwhat you need to remember is that minit and mshutdown are called in the opposite order that they are loaded, but you can’t depend on other extensions being around unless you have registered deps (remember way back in the main struct when we talked about deps?
  33. There are multiple declarations to help with this – the ZEND_STRS is a helper macro – again macro hell
  34. Class constants are another thing that’s pretty easy to do – global constants are evil, do NOT do
  35. Methods have several components but look very close to functions that we did earlier. There is a getThis which is available inside that will give you the zval for the class
  36. getTHis is the way you get “this” – notice that no matter how you declare your properties in MINIT, you can “juggle” the vale around depending on what you set it to
  37. these are some more advanced topics
  38. A PHP extension's globals are more properly called the "extension state", since most modules must remember what they're doing between function calls. The "counter" extension is a perfect example of this need: The basic interface calls for a counter with a persistent value. A programmer new to Zend and PHP might do something like this in counter.c to store that value:
  39. efree, emalloc, ecalloc – you can find good documentation on these manual/en/internals2.memory.management.phpzend is taking care of all the memory stuff for you – allocating a larger chunk, cleaning up at the end of the request (but still, efree dammit)Often, memory needs to be allocated for longer than the duration of a single request. These types of allocations, called persistent allocations because they persist beyond the end of a request, could be performed using the traditional memory allocators because these do not add the additional per-request information used by ZendMM. Sometimes, however, it's not known until runtime whether a particular allocation will need to be persistent or not, so ZendMM exports a set of helper macros that act just like the other memory allocation functions, but have an additional parameter at the end to indicate persistence.If you genuinely want a persistent allocation, this parameter should be set to one, in which case the request will be passed through to the traditional malloc() family of allocators. If runtime logic has determined that this block does not need to be persistent however, this parameter may be set to zero, and the call will be channeled to the perrequest memory allocator functions.
  40. The new resources – generally if you need to pass around some kind of internal C pointer stuff, this is the way you do itso we define a struct that holds our boring zend_object AND any additional C magic thingies we needwe’ll need a custom constructor for this – a C level constructor, that takes care of setting everything up, etcthere’s a bunch more abo
  41. you’ll do a lot of zend_fetch_object stuff to pick this stuff
  42. these are all “magic” methods you can plug into at the C level (note that you can also provide standard __get, __set etc, but extending classes can override them)you can even do casting magic, closure magic, even meddle with gc stuff – but most people only really do the clone, debug info (for var_dump), and get/set property stuff
  43. Why do you need to care about this? because of mod_apache!!!do you really need ot know how this works? nowhat DO You need to know – pass this stuff around, never use TSRMLS_FETCH – that is the evilest macro ever!PHP takes care of all of this for you – this is normally the HARD stuffrule of thumb #1 – stick an “e” in front of any memory stuff (emalloc, efree vs. malloc, free)Most of the time you won’t need to do much memory management, you’ll be using zend_parse_parameter stuff and zval manipulation which will do all of that for you – you MIGHT need it for whatever library your wrapping, but that can be very individualizedTURN ON DEBUG, turn on crt debug (windows), turn on memory leak detection, compile with all errors, use static analysis if you can (clang, /analyze for windows, etc)all PHP_METHOD and PHP_FUNCTIO nstuff you’ll do has TSRMLS stuff in there – when you write your own functions in C you call from elsewhere PASS THAT DAMN THING ALONG – TSRMLS_FETCH should almost NEVER be used (unless you’re writing a threading extension and then – well good luck with that)PHP has a special way of doing globals in an extension that abstracts away the “owie” parts, USE IT – unless you need truly global globals and then you damn well better be locking them! (and you’re doing something crazy at that point)
  44. Documentation is probably the most important part of the process, but I’ll still leave it for lastIf you’ve been writing examples and tests and protos and arginfo all along as I’ve whined about, you’re already 90 percent of the way there with docs, seriouslythe easiest way to do this would be to use a generator, however there aren’t any GOOD generators available right nowso just go to svn and copy another extensions stuff – everything is created with PhD so you can test stufffor pecl you can put your code anywhere, for phpdoc you need to get your docs roughed out then ask them to be put in svn
  45. Go into here about how resources work (an integer pointer to a global table holding resources… or doing some other evil resource storage – basically you’re storing and throwing about pointers and they’re slow (DO NOT)ini entries are a horrible thing – the use cases are extremely smallthreading and such I can rant about for quite a bitand finally you can override parts of the engine from extensions – this is why xdebug and things like intercept work but they’re a bit beyond the scope of this talk
  46. There is SOOO much more you can do from hooking objects to hooking the engine!
  47. There is SOOO much more you can do from hooking objects to hooking the engine!
  48. remember that a lot of the information on the internet and looking at extensions is out of date – these are some stuff that can help you, but your own experiences blogged would be great as wellI’m always sitting around in IRC willing to help out with things