SlideShare a Scribd company logo
1 of 82
PHP EXTENSIONS
Making the language even better
Congratulations!
• You’ve decided to learn “down the stack”
• You’ll learn more than you do “across the stack”
• Your brain will hurt a bit
• This will require you to be involved in your own learning
process
• It is HARDWORK to be good at anything new
WARNING!
The things shown here to apply to PHP 7 only!
Why do extensions?
• Talk to a C or C++ library
• Modify the way the engine works (zend extensions
usually)
• Make slow parts of code run faster
But I want to change the
engine!
Baby steps, baby steps
Wrapper
Extension
Speed and/or
Algorithm
Extension
Zend Extension
SAPI
Engine - Lexer,
AST, Parser
But I don’t know C!
1. compiled
2. strictly typed
3. php internals do “hard” stuff
4. copy and paste! boilerplate and macros
5. cairo, pecl_http, date
6. Don’t do the hard stuff first!
https://lxr.php.net
But I want to learn C
Also try:
• http://www.learn-c.org/
• http://c.learncodethehardway.org/book/
• http://aelinik.free.fr/c/
Practice Practice Practice!
Preparing to Compile
The big list
Quick Setup Needs
1. compiler
2. sdk
3. tools
4. dependencies
5. code
phpize is your friend!
How to Compile an Extension
1. phpize
2. ./configure
3. make
4. make install
5. make test
configure might require –with-php-config=/path/to/something
but…
But wait – there’s more!
• Compile your own PHP source, use a –prefix
• /usr/local/php-7.0-debug-zts is one I use
• I also have 20 php’s installed 
• We want developer specific flags
• --enable-maintainer-zts and --enable-debug
• optionally –enable-gcov
• install gdb and valgrind and optionally lcov
• this is easy on Ubuntu and similar systems where packages are easy to get
• You can also compile php with clang instead of gcc and do things like use static
analysis
Rasmus to the Rescue!
• https://github.com/rlerdorf/php7dev/blob/master/README.md
tl;dr
1. install virtualbox
2. install vagrant
3. git clone https://github.com/rlerdorf/php7dev.git
4. cd php7dev
5. vagrant up
6. vagrant ssh
7. makephp 7
How to Play along
git clone https://github.com/auroraeosrose/php-extensions-code.git
git checkout scaffolding
phpize
./configure
make
make test
Let’s write an extension!
But wait – there’s more….
1. Set up compile environment
2. Write a module definition
3. Learn about the PHP lifecycle
4. Learn about zvals
5. Add functions
6. ???
7. Profit!
Every Other ExtensionsTalk
DOIN IT RONG
Which doYOU want?
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!
Step 1. Why?
What is FFI?
• Foreign function interface
• Java calls it JNI
• HHVM calls it HNI
• Python calls it “ctypes” (do not ask, stupidest name ever)
• C# calls it P/Invoke
• Ruby calls it FFI
• PHP calls it…
FFI
Oh wait…
• php’s ffi is rather broken (last release is 2004-01-20)
• php’s ffi has no maintainer (ilia and wez were doing it)
• It needs someTLC
• There’s MFFI but it’s not done
• https://github.com/mgdm/MFFI
• Are you interested and not afraid? See me!
1. I hate parsing URIs
2. PHP’s parse_url is… not the best
3. How about an extension that wraps something
that parses uris in an excellent fashion
4. RESEARCHTIME
Step 2.What
Google says…
C library to the rescue!
• uriparser.sourceforge.net
• strictly RFC 3986 compliant
• available on many systems as packages
• cross platform
• documented http://uriparser.sourceforge.net/doc/html/
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 youWANT it to work
4. Write more then one example!
5. Turn these examples into your first tests
6. Yes this isTDD
Step 3. How
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!
Step 6. Extension Scaffolding
Configure files
Module struct
A word about macros
• Preprocessor directives are lines included in a program that
being with the character #, which make them different from a
typical source code text.They are invoked by the compiler to
process some programs before compilation.
• A macro is a fragment of code which has been given a name.
Whenever the name is used, it is replaced by the contents of
the macro.
PHP internals is Macro heaven!
• Or hell
• Use http://lxr.php.net to stay sane
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-
MASTER/CODING_STANDARDS
4. FOLLOWTHE CODING STANDARDS
5. use version control early on – github is easy!
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
Check if it works
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
Step 7.Write the test
PHPT tests
Let’s take a break here for some
semi-live demos…
Zval
The guts behind PHP’s type system
Typing systems compared
StaticTyping (C)
• Variables must be declared
before use
• Variables must be given a type at
declaration
• “compile time” checking
DynamicTyping (PHP)
• Variables don’t have to be
declared before use
• Variables can change types
• “run time” checking
zval
zval continued…
For more on the new zval
https://nikic.github.io/2015/05/05/Internal-value-representation-in-
PHP-7-part-1.html
long any numeric
double numeric with decimal
char* +
size_t
(length)
strings, binary
Hashtable dictionaries, arrays,structs
object any complicated type
For more on the new string type
http://jpauli.github.io/2015/09/18/php-string-management.html
How do I get C types from a ZVAL?
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_OBJ(zval) Z_OBJ_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)
My extension does nothing!
Back to our regularly schedule content
1. tell it to link to ourC library
2. add a function
3. learn how to get data
4. learn how to return data
Step 8. Actually do something
I hate autotools…
• Also known as the gnu build system
• provider of much pain, but much good use as well
• autoconf – generates a configure script (we cheat with phpize on shared
extensions)
• makes a configure we run to set up our information, mainly a config.h file
• automake – creates makefiles
• libtool – creates static and dynamic libraries
• Windows? Well php wrote it’s own version of autotools – in jscript (windows
javascript variant)
Anatomy of a Function
Wait – what was that?
• Define your function in C
• use a special call to parse parameters passed by the user
• use a special zval to return data to the user
• Tell PHP your extension provides this function
• put it in your “giant struct of doom” that lists them all
• send it to php when in your module struct
• Tell PHP what arguments your extension provides
• If your argument information and zpp argue users will be angry
• Yes it sucks you can’t just do one 
php type code
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 zend_long
integer L zend_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*, size_t
resource r zval *
string (possible null bytes) s char*, size_t
actual zval z zval *
actual zval Z zval**
zend_parse_parameters
type code
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
return_value
RETURN_RES(l)
RETURN_BOOL(b)
RETURN_NULL()
RETURN_LONG(l)
RETURN_DOUBLE(d)
RETURN_STRING(s)
RETURN_STRINGL(s, l)
RETURN_EMPTY_STRING()
RETURN_ZVAL(zv, copy, dtor)
RETURN_FALSE
RETURN_TRUE
RETVAL_RES(l)
RETVAL_BOOL(b)
RETVAL_NULL()
RETVAL_LONG(l)
RETVAL_DOUBLE(d)
RETVAL_STRING(s)
RETVAL_STRINGL(s, l)
RETVAL_EMPTY_STRING()
RETVAL_ZVAL(zv, copy, dtor)
RETVAL_FALSE
RETVAL_TRUE
Complex Data
array_init()
add_(index|assoc)_long()
add_(index|assoc)_bool()
add_(index|assoc)_string()
object_init()
add_property_long()
add_property_bool()
add_property_string()
Now to visit the attach-library
branch…
1. namespaces
2. classes
3. methods
4. constants
Step 9. Make it shiny
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)
add-basic-class branch
Lifecycle
Let’s take a moment to learn about how PHP works internally
Lifecycle
PHP stops
MSHUTDOWN – for each extension
RSHUTDOWN – for each request
cleanup after test.php
RINIT – for each request
execute test.php
MINIT – for each extension
request/parse test.php
PHP starts
php test.php
LifecycleThreaded
PHP stops
MSHUTDOWN – for each extension
request index.php request foo.php
RINIT – for each request
• execute test.php
RSHUTDOWN – for each request
• cleanup after test.php
RINIT – for each request
• execute test.php
RSHUTDOWN – for each request
• cleanup after test.php
MINIT – for each extension
apache starts
Class Properties
add-class-properties branch
Class Constants
add-class-constants branch
My class does nothing!
Methods are where it’s at
Class Methods
add-class-methods branch
Alter $this
manipulate this branch
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)
1. globals
2. memory management
3. custom objects
4. object handlers
5. thread safety
Step 10: Advanced topics
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
GlobalVariables (threads = evil)
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!
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)
Document, PECL, release
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
5. streams and transports
6. Complex objects with handlers
About Me
 http://emsmith.net
 auroraeosrose@gmail.com
 twitter - @auroraeosrose
 IRC – freenode – auroraeosrose
 #phpmentoring
 https://joind.in/talk/81b87
Questions?
HELPWITH DOCS!
http://edit.php.net
http://wiki.php.net/internals
HELPWITHTHE FUTURE
http://gophp7.org/gophp7-ext

More Related Content

What's hot

ShaREing Is Caring
ShaREing Is CaringShaREing Is Caring
ShaREing Is Caring
sporst
 
PHP7 - The New Engine for old good train
PHP7 - The New Engine for old good trainPHP7 - The New Engine for old good train
PHP7 - The New Engine for old good train
Xinchen Hui
 

What's hot (20)

On the Edge Systems Administration with Golang
On the Edge Systems Administration with GolangOn the Edge Systems Administration with Golang
On the Edge Systems Administration with Golang
 
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
 
Packer Genetics: The selfish code
Packer Genetics: The selfish codePacker Genetics: The selfish code
Packer Genetics: The selfish code
 
ShaREing Is Caring
ShaREing Is CaringShaREing Is Caring
ShaREing Is Caring
 
2021.laravelconf.tw.slides2
2021.laravelconf.tw.slides22021.laravelconf.tw.slides2
2021.laravelconf.tw.slides2
 
PHP7 - The New Engine for old good train
PHP7 - The New Engine for old good trainPHP7 - The New Engine for old good train
PHP7 - The New Engine for old good train
 
PHP, Under The Hood - DPC
PHP, Under The Hood - DPCPHP, Under The Hood - DPC
PHP, Under The Hood - DPC
 
Asynchronous I/O in Python 3
Asynchronous I/O in Python 3Asynchronous I/O in Python 3
Asynchronous I/O in Python 3
 
Introduction to Python for Security Professionals
Introduction to Python for Security ProfessionalsIntroduction to Python for Security Professionals
Introduction to Python for Security Professionals
 
Modern Black Mages Fighting in the Real World
Modern Black Mages Fighting in the Real WorldModern Black Mages Fighting in the Real World
Modern Black Mages Fighting in the Real World
 
Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016
 
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
 
Learning Python from Data
Learning Python from DataLearning Python from Data
Learning Python from Data
 
TypeProf for IDE: Enrich Development Experience without Annotations
TypeProf for IDE: Enrich Development Experience without AnnotationsTypeProf for IDE: Enrich Development Experience without Annotations
TypeProf for IDE: Enrich Development Experience without Annotations
 
A brief to PHP 7.3
A brief to PHP 7.3A brief to PHP 7.3
A brief to PHP 7.3
 
Perl Dist::Surveyor 2011
Perl Dist::Surveyor 2011Perl Dist::Surveyor 2011
Perl Dist::Surveyor 2011
 
PHP 7.1 : elegance of our legacy
PHP 7.1 : elegance of our legacyPHP 7.1 : elegance of our legacy
PHP 7.1 : elegance of our legacy
 
Static analysis for perl
Static analysis for perlStatic analysis for perl
Static analysis for perl
 
Go from a PHP Perspective
Go from a PHP PerspectiveGo from a PHP Perspective
Go from a PHP Perspective
 
Preparing code for Php 7 workshop
Preparing code for Php 7 workshopPreparing code for Php 7 workshop
Preparing code for Php 7 workshop
 

Viewers also liked

Viewers also liked (20)

SunshinePHP 2017 - Making the most out of MySQL
SunshinePHP 2017 - Making the most out of MySQLSunshinePHP 2017 - Making the most out of MySQL
SunshinePHP 2017 - Making the most out of MySQL
 
Debugging Effectively - SunshinePHP 2017
Debugging Effectively - SunshinePHP 2017Debugging Effectively - SunshinePHP 2017
Debugging Effectively - SunshinePHP 2017
 
A recommendation engine for your php application
A recommendation engine for your php applicationA recommendation engine for your php application
A recommendation engine for your php application
 
Debugging Effectively - PHP UK 2017
Debugging Effectively - PHP UK 2017Debugging Effectively - PHP UK 2017
Debugging Effectively - PHP UK 2017
 
Beyond Design Patterns and Principles - PHPBenelux 2017
Beyond Design Patterns and Principles - PHPBenelux 2017Beyond Design Patterns and Principles - PHPBenelux 2017
Beyond Design Patterns and Principles - PHPBenelux 2017
 
Docker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHPDocker for Developers - Sunshine PHP
Docker for Developers - Sunshine PHP
 
PHP Benelux 2017 - Caching The Right Way
PHP Benelux 2017 -  Caching The Right WayPHP Benelux 2017 -  Caching The Right Way
PHP Benelux 2017 - Caching The Right Way
 
Silo-Based Architectures for High Availability Applications @CodeCampCluj
Silo-Based Architectures for High Availability Applications @CodeCampClujSilo-Based Architectures for High Availability Applications @CodeCampCluj
Silo-Based Architectures for High Availability Applications @CodeCampCluj
 
Create, test, secure, repeat
Create, test, secure, repeatCreate, test, secure, repeat
Create, test, secure, repeat
 
php[world] 2015 Training - Laravel from the Ground Up
php[world] 2015 Training - Laravel from the Ground Upphp[world] 2015 Training - Laravel from the Ground Up
php[world] 2015 Training - Laravel from the Ground Up
 
Zend Framework Foundations
Zend Framework FoundationsZend Framework Foundations
Zend Framework Foundations
 
Amp your site an intro to accelerated mobile pages
Amp your site  an intro to accelerated mobile pagesAmp your site  an intro to accelerated mobile pages
Amp your site an intro to accelerated mobile pages
 
Engineer - Mastering the Art of Software
Engineer - Mastering the Art of SoftwareEngineer - Mastering the Art of Software
Engineer - Mastering the Art of Software
 
Hack the Future
Hack the FutureHack the Future
Hack the Future
 
Adding 1.21 Gigawatts to Applications with RabbitMQ (Bulgaria PHP 2016 - Tuto...
Adding 1.21 Gigawatts to Applications with RabbitMQ (Bulgaria PHP 2016 - Tuto...Adding 1.21 Gigawatts to Applications with RabbitMQ (Bulgaria PHP 2016 - Tuto...
Adding 1.21 Gigawatts to Applications with RabbitMQ (Bulgaria PHP 2016 - Tuto...
 
Git Empowered
Git EmpoweredGit Empowered
Git Empowered
 
Dip Your Toes in the Sea of Security
Dip Your Toes in the Sea of SecurityDip Your Toes in the Sea of Security
Dip Your Toes in the Sea of Security
 
Console Apps: php artisan forthe:win
Console Apps: php artisan forthe:win Console Apps: php artisan forthe:win
Console Apps: php artisan forthe:win
 
Presentation Bulgaria PHP
Presentation Bulgaria PHPPresentation Bulgaria PHP
Presentation Bulgaria PHP
 
Code Coverage for Total Security in Application Migrations
Code Coverage for Total Security in Application MigrationsCode Coverage for Total Security in Application Migrations
Code Coverage for Total Security in Application Migrations
 

Similar to Php extensions

Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for Dummies
Elizabeth Smith
 
Php extensions workshop
Php extensions workshopPhp extensions workshop
Php extensions workshop
julien pauli
 
Spl in the wild - zendcon2012
Spl in the wild - zendcon2012Spl in the wild - zendcon2012
Spl in the wild - zendcon2012
Elizabeth Smith
 
Php through the eyes of a hoster phpbnl11
Php through the eyes of a hoster phpbnl11Php through the eyes of a hoster phpbnl11
Php through the eyes of a hoster phpbnl11
Combell NV
 
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
goccy
 

Similar to Php extensions (20)

Php extensions
Php extensionsPhp extensions
Php extensions
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for Dummies
 
Php extensions workshop
Php extensions workshopPhp extensions workshop
Php extensions workshop
 
Spl in the wild - zendcon2012
Spl in the wild - zendcon2012Spl in the wild - zendcon2012
Spl in the wild - zendcon2012
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Php7 extensions workshop
Php7 extensions workshopPhp7 extensions workshop
Php7 extensions workshop
 
Using spl tools in your code
Using spl tools in your codeUsing spl tools in your code
Using spl tools in your code
 
Php through the eyes of a hoster
Php through the eyes of a hosterPhp through the eyes of a hoster
Php through the eyes of a hoster
 
Spl in the wild
Spl in the wildSpl in the wild
Spl in the wild
 
Php through the eyes of a hoster phpbnl11
Php through the eyes of a hoster phpbnl11Php through the eyes of a hoster phpbnl11
Php through the eyes of a hoster phpbnl11
 
Gcrc talk
Gcrc talkGcrc talk
Gcrc talk
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
 
Php through the eyes of a hoster pbc10
Php through the eyes of a hoster pbc10Php through the eyes of a hoster pbc10
Php through the eyes of a hoster pbc10
 
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)これからのPerlプロダクトのかたち(YAPC::Asia 2013)
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
 
The why and how of moving to php 5.4
The why and how of moving to php 5.4The why and how of moving to php 5.4
The why and how of moving to php 5.4
 
Php through the eyes of a hoster confoo
Php through the eyes of a hoster confooPhp through the eyes of a hoster confoo
Php through the eyes of a hoster confoo
 
Packaging perl (LPW2010)
Packaging perl (LPW2010)Packaging perl (LPW2010)
Packaging perl (LPW2010)
 
The why and how of moving to php 7
The why and how of moving to php 7The why and how of moving to php 7
The why and how of moving to php 7
 
More on bpftrace for MariaDB DBAs and Developers - FOSDEM 2022 MariaDB Devroom
More on bpftrace for MariaDB DBAs and Developers - FOSDEM 2022 MariaDB DevroomMore on bpftrace for MariaDB DBAs and Developers - FOSDEM 2022 MariaDB Devroom
More on bpftrace for MariaDB DBAs and Developers - FOSDEM 2022 MariaDB Devroom
 
From SaltStack to Puppet and beyond...
From SaltStack to Puppet and beyond...From SaltStack to Puppet and beyond...
From SaltStack to Puppet and beyond...
 

More from Elizabeth Smith

Security is not a feature
Security is not a featureSecurity is not a feature
Security is not a feature
Elizabeth Smith
 
Socket programming with php
Socket programming with phpSocket programming with php
Socket programming with php
Elizabeth Smith
 
Mentoring developers - Zendcon 2012
Mentoring developers - Zendcon 2012Mentoring developers - Zendcon 2012
Mentoring developers - Zendcon 2012
Elizabeth 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 2012
Elizabeth Smith
 
Event and signal driven programming
Event and signal driven programmingEvent and signal driven programming
Event and signal driven programming
Elizabeth 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
 
Modern sql
Modern sqlModern sql
Modern sql
 
Taming the tiger - pnwphp
Taming the tiger - pnwphpTaming the tiger - pnwphp
Taming the tiger - pnwphp
 
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
 
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
 
Mentoring developers
Mentoring developersMentoring developers
Mentoring developers
 
Event and signal driven programming
Event and signal driven programmingEvent and signal driven programming
Event and signal driven programming
 
Writing and using php streams and sockets tek11
Writing and using php streams and sockets   tek11Writing and using php streams and sockets   tek11
Writing and using php streams and sockets tek11
 
Php go vrooom!
Php go vrooom!Php go vrooom!
Php go vrooom!
 
SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09SPL to the Rescue - Tek 09
SPL to the Rescue - Tek 09
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 

Php extensions

  • 1. PHP EXTENSIONS Making the language even better
  • 2. Congratulations! • You’ve decided to learn “down the stack” • You’ll learn more than you do “across the stack” • Your brain will hurt a bit • This will require you to be involved in your own learning process • It is HARDWORK to be good at anything new
  • 3. WARNING! The things shown here to apply to PHP 7 only!
  • 4. Why do extensions? • Talk to a C or C++ library • Modify the way the engine works (zend extensions usually) • Make slow parts of code run faster
  • 5. But I want to change the engine! Baby steps, baby steps
  • 7. But I don’t know C! 1. compiled 2. strictly typed 3. php internals do “hard” stuff 4. copy and paste! boilerplate and macros 5. cairo, pecl_http, date 6. Don’t do the hard stuff first! https://lxr.php.net
  • 8. But I want to learn C
  • 9. Also try: • http://www.learn-c.org/ • http://c.learncodethehardway.org/book/ • http://aelinik.free.fr/c/ Practice Practice Practice!
  • 11. Quick Setup Needs 1. compiler 2. sdk 3. tools 4. dependencies 5. code phpize is your friend!
  • 12. How to Compile an Extension 1. phpize 2. ./configure 3. make 4. make install 5. make test configure might require –with-php-config=/path/to/something but…
  • 13. But wait – there’s more! • Compile your own PHP source, use a –prefix • /usr/local/php-7.0-debug-zts is one I use • I also have 20 php’s installed  • We want developer specific flags • --enable-maintainer-zts and --enable-debug • optionally –enable-gcov • install gdb and valgrind and optionally lcov • this is easy on Ubuntu and similar systems where packages are easy to get • You can also compile php with clang instead of gcc and do things like use static analysis
  • 14. Rasmus to the Rescue! • https://github.com/rlerdorf/php7dev/blob/master/README.md tl;dr 1. install virtualbox 2. install vagrant 3. git clone https://github.com/rlerdorf/php7dev.git 4. cd php7dev 5. vagrant up 6. vagrant ssh 7. makephp 7
  • 15. How to Play along git clone https://github.com/auroraeosrose/php-extensions-code.git git checkout scaffolding phpize ./configure make make test
  • 16. Let’s write an extension! But wait – there’s more….
  • 17. 1. Set up compile environment 2. Write a module definition 3. Learn about the PHP lifecycle 4. Learn about zvals 5. Add functions 6. ??? 7. Profit! Every Other ExtensionsTalk
  • 20. 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! Step 1. Why?
  • 21. What is FFI? • Foreign function interface • Java calls it JNI • HHVM calls it HNI • Python calls it “ctypes” (do not ask, stupidest name ever) • C# calls it P/Invoke • Ruby calls it FFI • PHP calls it…
  • 22. FFI
  • 23. Oh wait… • php’s ffi is rather broken (last release is 2004-01-20) • php’s ffi has no maintainer (ilia and wez were doing it) • It needs someTLC • There’s MFFI but it’s not done • https://github.com/mgdm/MFFI • Are you interested and not afraid? See me!
  • 24. 1. I hate parsing URIs 2. PHP’s parse_url is… not the best 3. How about an extension that wraps something that parses uris in an excellent fashion 4. RESEARCHTIME Step 2.What
  • 26. C library to the rescue! • uriparser.sourceforge.net • strictly RFC 3986 compliant • available on many systems as packages • cross platform • documented http://uriparser.sourceforge.net/doc/html/
  • 27. 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 youWANT it to work 4. Write more then one example! 5. Turn these examples into your first tests 6. Yes this isTDD Step 3. How
  • 28.
  • 29. 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! Step 6. Extension Scaffolding
  • 32. A word about macros • Preprocessor directives are lines included in a program that being with the character #, which make them different from a typical source code text.They are invoked by the compiler to process some programs before compilation. • A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro.
  • 33. PHP internals is Macro heaven! • Or hell • Use http://lxr.php.net to stay sane
  • 34. 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- MASTER/CODING_STANDARDS 4. FOLLOWTHE CODING STANDARDS 5. use version control early on – github is easy!
  • 35. 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 Check if it works
  • 36. 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 Step 7.Write the test
  • 38. Let’s take a break here for some semi-live demos…
  • 39. Zval The guts behind PHP’s type system
  • 40. Typing systems compared StaticTyping (C) • Variables must be declared before use • Variables must be given a type at declaration • “compile time” checking DynamicTyping (PHP) • Variables don’t have to be declared before use • Variables can change types • “run time” checking
  • 41. zval
  • 43. For more on the new zval https://nikic.github.io/2015/05/05/Internal-value-representation-in- PHP-7-part-1.html
  • 44. long any numeric double numeric with decimal char* + size_t (length) strings, binary Hashtable dictionaries, arrays,structs object any complicated type
  • 45. For more on the new string type http://jpauli.github.io/2015/09/18/php-string-management.html
  • 46. How do I get C types from a ZVAL? 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_OBJ(zval) Z_OBJ_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)
  • 47. My extension does nothing! Back to our regularly schedule content
  • 48. 1. tell it to link to ourC library 2. add a function 3. learn how to get data 4. learn how to return data Step 8. Actually do something
  • 49. I hate autotools… • Also known as the gnu build system • provider of much pain, but much good use as well • autoconf – generates a configure script (we cheat with phpize on shared extensions) • makes a configure we run to set up our information, mainly a config.h file • automake – creates makefiles • libtool – creates static and dynamic libraries • Windows? Well php wrote it’s own version of autotools – in jscript (windows javascript variant)
  • 50. Anatomy of a Function
  • 51. Wait – what was that? • Define your function in C • use a special call to parse parameters passed by the user • use a special zval to return data to the user • Tell PHP your extension provides this function • put it in your “giant struct of doom” that lists them all • send it to php when in your module struct • Tell PHP what arguments your extension provides • If your argument information and zpp argue users will be angry • Yes it sucks you can’t just do one 
  • 52. php type code 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 zend_long integer L zend_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*, size_t resource r zval * string (possible null bytes) s char*, size_t actual zval z zval * actual zval Z zval**
  • 53. zend_parse_parameters type code 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
  • 54. return_value RETURN_RES(l) RETURN_BOOL(b) RETURN_NULL() RETURN_LONG(l) RETURN_DOUBLE(d) RETURN_STRING(s) RETURN_STRINGL(s, l) RETURN_EMPTY_STRING() RETURN_ZVAL(zv, copy, dtor) RETURN_FALSE RETURN_TRUE RETVAL_RES(l) RETVAL_BOOL(b) RETVAL_NULL() RETVAL_LONG(l) RETVAL_DOUBLE(d) RETVAL_STRING(s) RETVAL_STRINGL(s, l) RETVAL_EMPTY_STRING() RETVAL_ZVAL(zv, copy, dtor) RETVAL_FALSE RETVAL_TRUE
  • 56. Now to visit the attach-library branch…
  • 57. 1. namespaces 2. classes 3. methods 4. constants Step 9. Make it shiny
  • 58. 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)
  • 59.
  • 60.
  • 62. Lifecycle Let’s take a moment to learn about how PHP works internally
  • 63. Lifecycle PHP stops MSHUTDOWN – for each extension RSHUTDOWN – for each request cleanup after test.php RINIT – for each request execute test.php MINIT – for each extension request/parse test.php PHP starts php test.php
  • 64. LifecycleThreaded PHP stops MSHUTDOWN – for each extension request index.php request foo.php RINIT – for each request • execute test.php RSHUTDOWN – for each request • cleanup after test.php RINIT – for each request • execute test.php RSHUTDOWN – for each request • cleanup after test.php MINIT – for each extension apache starts
  • 69. My class does nothing! Methods are where it’s at
  • 74. 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)
  • 75. 1. globals 2. memory management 3. custom objects 4. object handlers 5. thread safety Step 10: Advanced topics
  • 76. 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 GlobalVariables (threads = evil)
  • 77. 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
  • 78. 1. clean up what you emalloc (C level destructor) 2. read wiki on how to make them extendable!
  • 79. 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) Document, PECL, release
  • 80. 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 5. streams and transports 6. Complex objects with handlers
  • 81. About Me  http://emsmith.net  auroraeosrose@gmail.com  twitter - @auroraeosrose  IRC – freenode – auroraeosrose  #phpmentoring  https://joind.in/talk/81b87

Editor's Notes

  1. I mentor PHP developers into becoming C developers – because other PHP devs have done that for me If 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 project and looking at extensions makes me sad – 99% suck
  2. Give my short talk about why learning “down the stack” is so important and why more developers need to stop asking should I learn python or perl it’s better to learn in a domain entirely different than what you are currently doing if you do desktop learn web, if you learn web do desktop or mobile if you do PHP, don’t learn “across” (perl, python, ruby) learn DOWN java, C#, go, rust obj-c, C, c++ assembler the old gurus of C who built what we play with are dying off (do the math, how old is rasmus?) there won’t be hand holding, but a good mentor (for now me) will help you learn what you need to know
  3. PHP 7 will be breaking all the rules here, changing things in a fundamental manner, just something to keep in mind We’re working on a project to make migrating to PHP7 well documented and easy when the time comes and also working on ways to improve extensions in general
  4. This is the reason we have PHP extensions to hit the lower level parts of the code that are so important But the top reason is the #1 reason we write PHP extensions
  5. This is usually the first thing people think of when they want to “get involved with PHP internals” But you will have a far easier time of things if you start with extensions
  6. This is the proper path to get involved in PHP and the language - learning each piece of the stack step by step will make you not only a better program, and understand the language but it will make you a better part of the community
  7. This is the #1 argument I hear – but really is not anything you need to worry about c98 people! you can use some c99 stuff but declarations MUST go at the top of blocks (that’s really good coding standards anyway) although this will be moot when Microsoft finally gets it’s CTP for 2015 just like php itself turn all your errors on when compiling (-wall is your friend) and try to code clean you don’t have to “know C” anymore then you have to “know PHP” to write a wordpress plugin if you have the very basics of what to do it’s not hard PHP 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 objects Unless 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 test btw, all the code generators out there currently suck – most don’t do test generation, doc generation, or use the proper apis
  8. This is the book you want it’s affectionately called the C bible if you walk through this book and do the exercise you’ll get the basics of C
  9. These are some pretty good free resources for learning C – walking through the two books and the online tutorial will get you a good portion of the way there remember programming is thinking as much as writing code as if you play piano or dance you must practice practice practice
  10. So you’ll need to actually have an environment set up to compile PHP on I will say right now for your first foray into extensions you will be happiest on linux a VM is fine, in a debian/ubuntuish flavor because you’ll get a lot more help and support Also programming for windows and OSX is something you’ll need to learn before you finalize that extension but are much harder (yes, even OSX is harder – to do it right on there you use homebrew or something similar to install a linux stack – would be easier at first just to set up a linux vm)
  11. 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 system You 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 windows Then you need autotools – bison, and re2c to build PHP On 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
  12. 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-config Note this does shared extensions only – then again most of the extensions you’ll work on should probably be shared
  13. So although I wont’ whine about it today – if you really want to do extensions properly you need to have your own php compile you need to not be afraid of gcc and clang you need to make sure you test and test and test
  14. There, now we’ve switched to using a precompiled debug zts version of php 7.0 WOOHOO There aren’t (yet) other box versions/providers available – this is ongoing work (more on gophp7-ext later)
  15. 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 play These aren’t necessarily complicated extensions but they do follow PHP CS, have all the copy and paste you need and do compile (I think)
  16. So the part we’re all excited about – let’s write an extension! I’ll just get some code and then you copy and compile it and then…
  17. If you’ve ever seen another PHP extensions talk they dive into how to use the zend engine and how things work and such PHP 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 ;)
  18. In addition this is the absolutely wrong way to approach writing PHP extensions! Actually it’s the wrong way to approach writing LOTS of things do 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 product in a different language doesn’t mean the rules change
  19. 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 Please be the solution, not the problem We need no more painful APIs in PHP
  20. so these are the reasons you’d write a PHP extension as we talked about before – you want to use a C library, you want to make something process intensive faster, you want to hook into the engine itself there aren’t other reasons to do an extension wait? what is FFI?
  21. So many languages support this idea of calling into usually C code Then they usually put a layer of regular code “on top” to make APIS non painful This can be useful it also tends to be slower
  22. 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 ffi I 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”
  23. oh ffi is cool and so needs a maintainer
  24. so this is what we want to do – write an extension that wraps a good C uri parsing library
  25. so you need to do your research right? there are multiple C parsing libraries, joyent has one and there are some nice c++ ones, but for simple well laid out C uriparser seems to win – also it’s really nice use your internet tools – when you started PHP I’m sure you had books and stuff on speed dial you will need to do the same for C
  26. SO this is a VERY nice C library for parsing uris according to the spec – USEFUL AS ALL GETOUT cross platform, well maintained, easy to use – don’t let the sourceforge URL fool you, it’s in git 
  27. 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 well really 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
  28. uriparser.sourceforge.net – a simple uri parser library that strictly conforms to rfc questions to ask yourself as you write these examples do I want namespaces? will this be an object oriented api or a procedural style 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
  29. PHP is 90% copy and paste, the main copy and paste stuff is all scaffolding for an extension so 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 it talk 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
  30. DO BOTH unless you’re absolutely certain that whatever you’re writing will only work on one system or the other m4 is autotools stuff, but the w32 are very easy to write as well, they’re just jscript (windows javascript dialect) with some special functions for things like checking headers and adding libs, etc talk about with vs. enable
  31. this is the very basic items you can have in your struct to define what you module is and what it does STANDARD_MODULE_HEADER_EX allows you to add ini entries and depedencies to your module definition using STANDARD_MODULE_PROPERTIES_EX allows you to add globals what the globals are, a ginit and a gshutdown – plus a “post deactivate” method that basically never gets used ;)
  32. Explain what these are to the C uniunitiated
  33. Just some general rules for you to take a look at even if your extension is not going into PHP proper the more time you take to properly document your code the happier you will be
  34. how to see if what you’ve been playing with will actually work This should load your boring extension without doing anything fancy, just allow it to show up in the modules list you can actually do a test case for this with extension_loaded or get_loaded_modules if you’re so inclined
  35. everybody should ALWAYS test their extensions – test test test turn debugging on, turn memory leaks on - run your tests if 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 .sh file to run the test exactly like it was run the first time no .bat file yet… yes I have a patch for that
  36. These are very basic functional style tests there are a bunch of different sections for each one – the important thing to remember is always include a skipif anything in “tests” will be run so it’s ok to put things in directories for order write tests for everything you can think of! this is my standard test to check my phpinfo (MINFO) function
  37. Look at the code and look at the compile
  38. NOTE: again there were many underlying changes made to php’s zvals for php 7 I’m not going to go into all the changes but I may mention a few we’re going to detour just a moment to learn something about PHP’s guts
  39. But wait – PHP is sitting on C we are somehow translating C types to PHP types – how is this magic accomplished?
  40. Our zval is a giant struct of doom for 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!
  41. PHP 7 has totally flipped around how a zval is structured
  42. PHP 7 has totally flipped around how a zval is structured Simple values do not require allocation and don’t use refcounting. There is no more double refcounting. In the object case, only the refcount in the object is used now. Because the refcount is now stored in the value itself, the value can be shared independently of the zval structure. A string can be used both in a zval and a hashtable key. There is a lot less indirection, i.e. the number of pointers you need to follow to get to a value is lower. Discuss heap vs. stack memory here
  43. So basically with an extension (especially one wrapping a library) what you’re doing is taking C variables and exposing them to php land by turning them into zvals 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
  44. In C, strings are simple NULL-terminated char arrays, like you know. However, when designing a scripting language such as PHP, the need to manage those strings arises. In management, we think about classical operations, such as concatenation, extension or truncation ; and eventually more advanced concepts, such as special allocation mechanisms, string interning or string compression. As length are typed on a size_t variable, they weight {platform size} bytes ! Whatever the platform. One of the PHP 5 problems is then solved : under a CPU64, string length will be 8 bytes (64 digits) for every platform (this is one of the definition of the C size_t type). Strings are refcounted ! In PHP 7, strings are refcounted (as well as many other primitive types). That means that interned strings are still relevant, but less : PHP layers can now pass strings from one to the other, as strings are refcounted, we are plainly sure that noone will accidentaly free the string as this latter is still used elsewhere (until doing an error on purpose).
  45. 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 stuff note there are even MORE then this, but these are the ones you’ll use most often PHP 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 The PP macros have been ripped out of php7 because we no longer have pointer pointers all over the place And the macros without the pointer are used a LOT more than they used to be Booleans have internally changed – you should always check for IS_FALSE and IS_TRUE instead of considering as a Boolean value, and doing an lval on it will break
  46. Now let’s make our extension like… actually do something
  47. 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
  48. Here show all the lines required to do one thing – link into the library mention how other options are thin on the ground, although cmake is gainin some major traction about 2008 there was an attempt to port php’s build system to cmake sadly cmake was not really ready for it it IS ready for it now (hhvm and others all use cmake) But again that’s a monstrous project and you’re fighting a battle gains? one build system for everything ,not maintaining our own configure system for windows, easier integration into ecosystems
  49. you need to do three things to add a function to your extension define it’s arguments – this is for reflection define it’s body put it inside a function struct arginfo must have the begin and end arginfo then you add additional arguments as desired in between if you have optional arguments, use begin_arginfo_ex you can send by val or reference or do typehinting with arginfo as well
  50. SO a lot of this is implementation detail issues so in PHP you use this macro – all it does is rename your function name to something that won’t collide evilly, and define a bunch of C parameters you’re getting passed in 
  51. there are even more than this in php7 strings move to use the more correct The IS_LONG type now uses a zend_long value instead of an ordinary C long. The reason behind this is that on 64-bit Windows (LLP64) a long is only 32-bit wide, so PHP 5 ended up always using 32-bit numbers on Windows. PHP 7 will allow you to use 64-bit numbers if you’re on an 64-bit operating system, even if that operating system is Windows.
  52. so you can also do “zend_parse_parameters_ex” the only thing you can do it with is QUIET there are a couple of reasons for doing this – mostly nesting and overloaded calls Note that variable args have dropped a pointer!
  53. This is macro hell – PHP is macro hell every 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 back it’s called return_value you manipulate these return value zvals to get data back into PHP userland the one thing that is Retval_res used to be retval_resource Retval string no longer has the duplicate option
  54. When you start to want to return complex data things get a little more complicated – this is where array and object init come in there 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 creating there are a bunch more of these zend_API.h is your lifeblood for how to use these things, along with Note that strings used to have the option of saying “duplicate me” or not, now you don’t worry about it, because you’re using a zend_string with refcounts and such
  55. Look at the code and look at the compile make clean, switch branch, You have to install uriparser by handls ls That means download and build – it has a pretty straightforward system You can –disable-doc and –disable- I forget (check in code) to ignore bad things
  56. Now we move on to more complex parts of the code, methods that really DO something remember we should start by writing tests for it
  57. Zend_object can be a variable length now! These can lead to far more complex code if you’re doing evil with objects We don’t really go over object handlers and other crazy stuff because that’s beyond the scope here of our learning, but something to look up
  58. TO do a class – create and register it’s CE in minit put it in another file – will keep yourself sane instead of having 300 mile long files some people whine that this makes it harder to use static stuff, since each file is compiled into it’s own “unit” and then linked but modern compilers and linkers make the overhead on this moot – better to be organized!
  59. so this is how you would do your minit function – this would go into php_uriparser.c where the other code displayed would be in uri.c – where all our code for our uri class is going it 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 other remember stuff must be declared in order to be used too! the namespace declaration and the PHP_MINIT declaration go into the header file I 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
  60. So this has ONLY the basics – explain how the minit works then let’s talk lifecycle
  61. Deep breath – let’s talk about those things we put in the module struct Each are run at a different point in time let’s also talk a wee bit about structure? sapis?
  62. so this is the lifecycle of PHP each of these items has a different important role to play what 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? this flow is also true for prefork php webservers But then we get into threads
  63. When we add threading it gets a little more difficult, because minit/mshutdown are only called when apache is restarted there’s also ginit and gshutdown but they’re broken
  64. There are multiple declarations to help with this – the ZEND_STRS is a helper macro – again macro hell
  65. So this has ONLY the basics – explain how the minit works then let’s talk lifecycle
  66. Class constants are another thing that’s pretty easy to do – global constants are evil, do NOT do
  67. then we add some constants for fun
  68. Now let’s make our extension like… actually do something
  69. 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 notice how similar this is to functions!
  70. go over the method stuff just a bit
  71. 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
  72. go over the method stuff just a bit
  73. Note – traits in extensions are basically broken 
  74. these are some more advanced topics
  75. 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: This is changed significantly in the way this is stored – as we’re using thread local globals in our thread safe build
  76. efree, emalloc, ecalloc – you can find good documentation on these manual/en/internals2.memory.management.php zend 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.
  77. you’ll do a lot of zend_fetch_object stuff to pick this stuff
  78. Documentation is probably the most important part of the process, but I’ll still leave it for last If 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, seriously the easiest way to do this would be to use a generator, however there aren’t any GOOD generators available right now so just go to svn and copy another extensions stuff – everything is created with PhD so you can test stuff for 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 also mention readthedocs and the fact that pecl has licensing restrictions and we need a packagist
  79. 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 small threading and such I can rant about for quite a bit and 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
  80. There is SOOO much more you can do from hooking objects to hooking the engine!
  81. There is SOOO much more you can do from hooking objects to hooking the engine!