0% found this document useful (0 votes)
89 views30 pages

Extensions Phpnw11

Derick Rethans gave a talk on PHP extensions. He discussed what extensions are, examples of core and PECL extensions, how extensions add functionality to PHP, and why developers create extensions, such as to wrap libraries, improve performance, or enable functionality not possible in PHP alone. The talk covered extension basics like initialization, replacing functions, and opcode overloading, as well as more advanced topics like streams and compiler caches. Resources for learning more about extensions and the extension mechanism were provided.

Uploaded by

Bryana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views30 pages

Extensions Phpnw11

Derick Rethans gave a talk on PHP extensions. He discussed what extensions are, examples of core and PECL extensions, how extensions add functionality to PHP, and why developers create extensions, such as to wrap libraries, improve performance, or enable functionality not possible in PHP alone. The talk covered extension basics like initialization, replacing functions, and opcode overloading, as well as more advanced topics like streams and compiler caches. Resources for learning more about extensions and the extension mechanism were provided.

Uploaded by

Bryana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Welcome!

PHP Extensions - What and


Why
PHP North West - Manchester, UK - Oct 8th, 2011
Derick Rethans - [email protected] - twitter:
@derickr
http://derickrethans.nl/talks.html
http://joind.in/3586

About Me

Derick Rethans
Dutchman living in London
PHP development
Author of the mcrypt, input_filter, dbus, translit
and date/time extensions
Author of Xdebug
Contributor to the Apache Zeta Components
Incubator project (formerly eZ Components)
Freelancer doing PHP (internals) development
Anderskor

Extension on a slide

What?

Extensions add functionality to PHP


Extensions replace functionality in PHP
PHP's extension mechanism is easy
PHP's extension mechanism is (too) powerful

Core

ext/standard
Contains all the default PHP functions
Can not be disabled
Other core extensions are:
ext/date
ext/ereg
ext/pcre
ext/reflection

Internal

Everything in ext/
Are compiled into PHP
Can most of the time be compiled as shared
extensions as well distributions like to do that
(too much)

External

Everything not in the core distribution


Are often not compiled into PHP, but instead are
compiled as shared objects
Sometimes make their way into the core
distribution, and sometimes they are moved away

PECL

PECL: PHP Extension C Library


http://pecl.php.net
A whole load of extensions for various interesting
(and odd things)
Are installed with the PECL tool
Used to be called "Siberia"
Don't really have to be hosted in PHP's SVN
repository, on the PECL website

A few examples...

Core distribution: bcmath bz2 calendar com_dotnet ctype curl date dba dom enchant ereg exif
fileinfo filter ftp gd gettext gmp hash iconv imap interbase intl json ldap libxml mbstring mcrypt
mssql mysql mysqli mysqlnd oci8 odbc openssl pcntl pcre pdo pdo_dblib pdo_firebird pdo_mysql
pdo_oci pdo_odbc pdo_pgsql pdo_sqlite pgsql phar posix pspell readline recode reflection session
shmop simplexml skeleton snmp soap sockets spl sqlite sqlite3 standard sybase_ct sysvmsg sysvsem
sysvshm tidy tokenizer wddx xml xmlreader xmlrpc xmlwriter xsl zip zlib
PECL: activescript adt amfext amqp apache_accessor apc apd apm archive ares automap axis2
bbcode bcompiler bitset blenc bloomy bz2_filter cairo chasen classkit colorer courierauth cpdf crack
cubrid cvsclient cybercash cybermut cyrus daffodildb date dazuko db dbase dbdo dbplus dbus dbx
demoext dio docblock domxml doublemetaphone drizzle dtrace ecasound enchant esmtp etpan event
expect fam fbsql fdf ffi filepro flitetts framegrab freeimage fribidi fuse gdchart gearman gender
geoip gmagick gnupg gupnp haru hidef html_parse htscanner http hwapi hyperwave ibm_db2 id3 idn
iisfunc imagick ims inclued informix ingres inotify intercept intl iptcdata ixsfunc judy kadm5 kakasi
krb5 ktaglib lchash libevent libexif litespeed llvm lua lzf mailparse markdown maxdb mcal
mcrypt_filter mcve mdbtools memcache memcached memsession memtrack ming mnogosearch
mono mqseries msession msql muscat myphp mysqlnd_mc mysqlnd_ms mysqlnd_pscache
mysqlnd_qc mysqlnd_sip mysqlnd_uh namazu ncurses Net_Gopher netools newt notes ntuser oauth
ocal oggvorbis openal opendirectory openssl operator optimizer oracle ovrimos pam paradox params
parsekit parse_tree pdf pdo_4d pdo_ibm pdo_ids pdo_informix pdo_user perl pfpro phar philter phk
phpdoc picosql pop3 prephp preprocessor printer proctitle ps python qtdom radius rar rb rpc
rpmreader rrd rsvg rsync runkit sam sasl satellite scream sdo selinux servlet session_pgsql shp
simple_cvs smbc smtp sndfile solr sphinx spl_types spplus spread sqlite ssdeep ssh2 statgrab stats
stem stomp svn swf swish sybase tcc tclink tcpwrap tdb tests textcat threads timezonedb translit
txforward unicode uploadprogress usblib uuid v8js valkyrie varnish vpopmail w32api wbxml weakref
win32ps win32scheduler win32service win32std wincache wxwidgets xattr xdiff xdom xhprof
xmgrace xml xmlrpci xmms xquery xrange xslcache xslt yaf yami yaml yaz yp zeroconf zip zlib_filter

A quick overview...

Traditional parts

Module init/shutdown
Request init/shutdown
Functions
Configuration (INI) settings

Modern parts

Classes and methods


Argument description
Dependencies on other extensions

Extension hooks

Normal extensions
Module and request init and shutdown; and post
module deinitialisation
phpinfo()
Zend extensions
statement calls
all sorts of internal handlers: activate/deactivate
functions, op_array handler, op_array constructor
and destructor

Overloading

Error handler: Xdebug, SOAP


Exception throwing handler
Compiler and executor: APC and other caches,
funcall, Xdebug
Setting of headers
Opcodes: Xdebug, operator

Opcode overloading

Opcodes you say?

Executing
In a diagram

Compiling: Diagram

Executing: Diagram

Compiler Caches
How it works

In general, each source file is compiled once


Compilation overhead becomes inconsequential
Cache introduces its own overhead due to
dynamic nature of includes

Opcode overloading

Scream: For making PHP ignore the @ operator


Xdebug: For code coverage, and for making PHP
ignore the @ operator
Operator: Allows for operator overloading, but
also demands that you hand over your first-born

Replacing functions

Replaces a function pointer in the symbol table


with a new one
You can also use it for methods
Xdebug uses it to replace var_dump with
xdebug_var_dump
You can only replace internal functions (unless
you're runkit/classkit)

Streams

Wrappers: Wrap around file reading and writing


operations with a "protocol" (f.e.:
http://compress.bzip2)
Filters: On the fly encrypting/decrypting filter
(f.e.: mcrypt_filter)

British PHP

<?php
$marmiteIsFor = 'trashcan';
echo 'Hello World.';
?>

<?php
marmiteIsFor = 'biscuits';
announce 'Good morrow, fellow subjects of the Crown.';
?>

Sadly, this you can't do.


Or can you?
Facebook's xhp works as a pre-processor...

Why extensions?
Wrapping libraries

General use libraries: re2


Specific use libraries: cybermut, pam
Libraries that you have written yourself, or your
company
Missing database support, or new webscale things
like mongodb

Why extensions?
They are faster than raw PHP code

It's just too slow in normal PHP: ssh2, openssl


Specific uses: QuickHash, StumbleCache

Why extensions?
Performance bottlenecks

Your code is really slow, and need something


quicker: twig

Why extensions?
Impossible in just PHP

Not everything is possible in pure PHP: apc, com,


xdebug, xhprof

Questions
?

Resources

These Slides: http://derickrethans.nl/talks.php


VLD: http://www.derickrethans.nl/vld.php
Xdebug: http://xdebug.org
Inclued: http://t3.dotgnu.info/blog/tags/inclued/
Extending and Embedding PHP ('Sara's Book'):
http://www.amazon.com/Extending-EmbeddingPHP-Sara-Golemon/dp/067232704X
Tutorial on Zend's devzone:
http://devzone.zend.com/article/1021

Thanks!

Derick Rethans - [email protected] - twitter:


@derickr
http://derickrethans.nl/talks.html
http://joind.in/3586

You might also like