php extensions

80
PHP EXTENSIONS Making the language even better

Upload: elizabeth-smith

Post on 16-Apr-2017

373 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: Php extensions

PHP EXTENSIONSMaking the language even better

Page 2: Php extensions

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 HARD WORK to be good at anything new

Page 3: Php extensions

WARNING!The things shown here to apply to PHP 7 only!

Page 4: Php extensions

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

Page 5: Php extensions

But I want to change the engine!Baby steps, baby steps

Page 6: Php extensions

Wrapper Extension

Speed and/or Algorithm Extension

Zend Extension

SAPI

Engine - Lexer, AST,

Parser

Page 7: Php extensions

But I don’t know C!1. compiled2. strictly typed3. php internals do “hard” stuff4. copy and paste! boilerplate and macros5. cairo, pecl_http, date6. Don’t do the hard stuff first!

lxr.php.nethttps://php-lxr.adamharvey.name/source/

Page 8: Php extensions

But I want to learn C

Page 9: Php extensions

Also try:• http://www.learn-c.org/ • http://c.learncodethehardway.org/book/ • http://aelinik.free.fr/c/

Practice Practice Practice!

Page 10: Php extensions

Preparing to CompileThe big list

Page 11: Php extensions

Quick Setup Needs1. compiler 2. sdk3. tools4. dependencies5. code

phpize is your friend!

Page 12: Php extensions

How to Compile an Extension1. phpize 2. ./configure3. make4. make install5. make test

configure might require –with-php-config=/path/to/somethingbut…

Page 13: Php extensions

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

Page 14: Php extensions

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

Page 15: Php extensions

How to Play alonggit clone https://github.com/auroraeosrose/php-extensions-code.git

git clone git://github.com/auroraeosrose/php-extensions-code.git git checkout scaffoldingphpize./configuremakemake test

Page 16: Php extensions

Let’s write an extension!But wait – there’s more….

Page 17: Php extensions

1. Set up compile environment2. Write a module definition3. Learn about the PHP lifecycle4. Learn about zvals5. Add functions6. ???7. Profit!

Every Other Extensions Talk

Page 18: Php extensions

DOIN IT RONG

Page 19: Php extensions

Which do YOU want?

Page 20: Php extensions

1. Do something you can’t do in userland2. Utilize a C library3. Make slow code faster

Maybe you should just use ffi!

Step 1. Why?

Page 21: Php extensions

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…

Page 22: Php extensions

FFI

Page 23: Php extensions

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 some TLC• There’s MFFI but it’s not done

https://github.com/mgdm/MFFI

• Are you interested and not afraid? See me!

Page 24: Php extensions

1. I hate parsing URIs2. PHP’s parse_url is… not the best3. How about an extension that wraps

something that parses uris in an excellent fashion

4. RESEARCH TIME

Step 2. What

Page 25: Php extensions

Google says…

Page 26: Php extensions

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/

Page 27: Php extensions

1. Think about what the API should be2. Look at the C APIs but don’t mimic them3. Write an example of how you WANT it to

work4. Write more then one example!5. Turn these examples into your first tests6. Yes this is TDD

Step 3. How

Page 28: Php extensions
Page 29: Php extensions

1. This is copy and paste1. config.m4 & config.w322. macros for version api changes if

necessary3. main module file (php_{$ext}.c)4. main header file (php_{$ext}.h)

2. Compile it and test!

Step 6. Extension Scaffolding

Page 30: Php extensions

Configure files

Page 31: Php extensions

Module struct

Page 32: Php extensions

Scaffolding rules1. make sure you name your files in a standard

way2. document! use a license header, proto

statements3. read the coding standards

http://lxr.php.net/xref/PHP_5_6/CODING_STANDARDS

4. FOLLOW THE CODING STANDARDS5. use version control early on – github is easy!

Page 33: Php extensions

php –d extension=myext.so –m

The scaffold extension should show up in the list

Make sure to 1. define a version constant2. read and use PHP code standards

Check if it works

Page 34: Php extensions

1. run-test.php2. 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

Page 35: Php extensions

PHPT tests

Page 36: Php extensions

Let’s take a break here for some semi-live demos…

Page 37: Php extensions

ZvalThe guts behind PHP’s type system

Page 38: Php extensions

Typing systems comparedStatic Typing (C)• Variables must be

declared before use• Variables must be given

a type at declaration• “compile time” checking

Dynamic Typing (PHP)• Variables don’t have to

be declared before use• Variables can change

types• “run time” checking

Page 39: Php extensions

zval

Page 40: Php extensions

zval continued…

Page 41: Php extensions

For more on the new zvalhttps://nikic.github.io/2015/05/05/Internal-value-representation-in-PHP-7-part-1.html

Page 42: Php extensions

long any numeric

double numeric with decimal

char* + size_t

(length)strings, binary

Hashtable dictionaries, arrays,structs

object any complicated type

Page 43: Php extensions

For more on the new string type

http://jpauli.github.io/2015/09/18/php-string-management.html

Page 44: Php extensions

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)

Page 45: Php extensions

My extension does nothing!Back to our regularly schedule content

Page 46: Php extensions

1. tell it to link to our C library2. add a function3. learn how to get data4. learn how to return data

Step 8. Actually do something

Page 47: Php extensions

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)

Page 48: Php extensions

Anatomy of a Function

Page 49: Php extensions

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

Page 50: Php extensions

php type code c typearray or object a zval *boolean b zend_boolclass C zend_class_entry *double d doublecallable f zend_fcall_info

andzend_fcall_info_cachearray or HASH_OF(object) H HashTable*

array h HashTable*integer l zend_longinteger L zend_long with LONG_MAX, LONG_MIN

limitsobject o zval *object of specific type O zval *, zend_class_entrystring (no null bytes) p char*, size_tresource r zval *string (possible null bytes)

s char*, size_t

actual zval z zval *actual zval Z zval**

Page 51: Php extensions

zend_parse_parameterstype code

variable args (any) * int, zval**

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

| anything after is optional, use defaults

/ use SEPARATE_ZVAL_IF_NOT_REFdoesn’t apply to b, l, and d ! C NULL for zval null

Page 52: Php extensions

return_valueRETURN_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

Page 53: Php extensions

Complex Dataarray_init(

)add_(index|assoc)_long()

add_(index|assoc)_bool()

add_(index|assoc)_string()

object_init()

add_property_long()

add_property_bool()

add_property_string()

Page 54: Php extensions

Now to visit the attach-library branch…

Page 55: Php extensions

1. namespaces2. classes3. methods4. constants

Step 9. Make it shiny

Page 56: Php extensions

Classes1. name, parent, and flags2. hashtables of methods, default methods,

static methods3. hashtables of static properties, default

properties, and properties4. object handlers 5. union of either file information, or

internal structures (for internal classes)

Page 57: Php extensions
Page 58: Php extensions
Page 59: Php extensions

add-basic-class branch

Page 60: Php extensions

LifecycleLet’s take a moment to learn about how PHP works internally

Page 61: Php extensions

Lifecycle

PHP stops

MSHUTDOWN – for each extension

RSHUTDOWN – for each requestcleanup after test.php

RINIT – for each requestexecute test.php

MINIT – for each extensionrequest/parse test.php

PHP startsphp test.php

Page 62: Php extensions

Lifecycle Threaded

PHP stops

MSHUTDOWN – for each extension

request index.php request foo.phpRINIT – 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

Page 63: Php extensions

Class Properties

Page 64: Php extensions

add-class-properties branch

Page 65: Php extensions

Class Constants

Page 66: Php extensions

add-class-constants branch

Page 67: Php extensions

My class does nothing!Methods are where it’s at

Page 68: Php extensions

Class Methods

Page 69: Php extensions

add-class-methods branch

Page 70: Php extensions

Alter $this

Page 71: Php extensions

manipulate this branch

Page 72: Php extensions

Abstract, Interface, TraitClass MethodZEND_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_PRIVATEZEND_ACC_CTORZEND_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)

Page 73: Php extensions

1. globals2. memory management3. custom objects4. object handlers5. thread safety

Step 10: Advanced topics

Page 74: Php extensions

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

Global Variables (threads = evil)

Page 75: Php extensions

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

Page 76: Php extensions

1. clean up what you emalloc (C level destructor)2. read wiki on how to make them extendable!

Page 77: Php extensions

1. http://edit.php.net 2. http://svn.php.net/viewvc/phpdoc/en/trunk/refere

nce/3. PhD is awesomesauce• http://doc.php.net/phd/

4. email [email protected] 1. who you are2. 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

Page 78: Php extensions

Stuff I didn’t talk about1. resources (use custom objects instead)2. ini entries (just DON’T)3. threading and parallel processing4. engine hooking5. streams and transports6. Complex objects with handlers

Page 79: Php extensions

About Me http://emsmith.net [email protected] twitter - @auroraeosrose IRC – freenode –

auroraeosrose #phpmentoring #gophp7-ext https://joind.in/talk/56f05

Page 80: Php extensions

Questions?

HELP WITH DOCS!http://edit.php.nethttp://wiki.php.net/internals

HELP WITH THE FUTURE

http://gophp7.org/gophp7-ext