Php extensions

  • View
    501

  • Download
    0

Embed Size (px)

Text of Php extensions

PHP EXTENSIONS

PHP EXTENSIONSMaking the language even betterI 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% suck1Congratulations!Youve decided to learn down the stackYoull learn more than you do across the stackYour brain will hurt a bitThis will require you to be involved in your own learning processIt is HARD WORK to be good at anything newGive my short talk about why learning down the stack is so important and why moredevelopers need to stop asking should I learn python or perl

its 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 mobileif you do PHP, dont learn across (perl, python, ruby) learn DOWNjava, C#, go, rustobj-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 wont be hand holding, but a good mentor (for now me) will help you learn what you need to know2WARNING!The things shown here to apply to PHP 5.4 PHP 5.xPHP 7 will be breaking all the rules here, changing things in a fundamental manner, just something to keep in mindWere working on a project to make migrating to PHP7 well documented and easy when the time comesand also working on ways to improve extensions in general

3Why do extensions?Talk to a C or C++ libraryModify the way the engine works (zend extensions usually)Make slow parts of code run fasterThis is the reason we have PHP extensionsto hit the lower level parts of the code that are so important

But the top reason is the #1 reason we write PHP extensions4But I want to change the engine!Baby steps, baby stepsThis is usually the first thing people think of when they want to get involved with PHP internalsBut you will have a far easier time of things if you start with extensions

5This is the proper path to get involved in PHP and the language - learning each piece of the stack step by step willmake you not only a better program, and understand the languagebut it will make you a better part of the community6But I dont know C!compiledstrictly typedphp internals do hard stuffcopy and paste! boilerplate and macroscairo, pecl_http, dateDont do the hard stuff first!

lxr.php.netThis 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 (thats really good coding standards anyway)although this will be moot when Microsoft finally gets its CTP for 2015

just like php itself turn all your errors on when compiling (-wall is your friend) and try to code clean

you dont have to know C anymore then you have to know PHP to write a wordpress pluginif you have the very basics of what to do its not hardPHP takes care of a lot of the heavy lifting from how to parse parameters coming in and how to shove data into someplace going outto how to do fancy objects

Unless youre 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, its just a matter of finding the code compile and test

btw, all the code generators out there currently suck most dont do test generation, doc generation, or use the proper apis7But I want to learn C

This is the book you wantits affectionately called the C bibleif you walk through this book and do the exercise youll get the basics of C

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

Practice Practice Practice!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 codeas if you play piano or dance you must practice practice practice

9Preparing to CompileThe big listSo youll need to actually have an environment set up to compile PHP onI will say right now for your first foray into extensions you will be happiest on linuxa VM is fine, in a debian/ubuntuish flavor because youll get a lot more help and support

Also programming for windows and OSX is something youll need to learn before you finalize that extension but are muchharder (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)10Quick Setup Needscompiler sdktoolsdependenciescode

phpize is your friend!This is the same and yet different on every system and were not going to go into it a whole lot this should have been homework before you came ;)

Basically you need a compiler xcode on mac, vc(2008) express on windows, gcc something on linux this is different for every systemYou also need the sdk and headers for your system those will be put on with xcode, youll 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

11How to Compile an Extensionphpize ./configuremakemake installmake test

configure might require with-php-config=/path/to/somethingbutmake 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 youll work on should probably be shared12But wait theres more!Compile your own PHP source, use a prefix/usr/local/php-5.6-debug-zts is one I useI also have 20 phps installed We want developer specific flags--enable-maintainer-zts and --enable-debug optionally enable-gcov install gdb and valgrind and optionally lcovthis is easy on Ubuntu and similar systems where packages are easy to getYou can also compile php with clang instead of gcc and do things like use static analysis

So although I wont whine about it today if you really want to do extensions properly you need to have your own php compileyou need to not be afraid of gcc and clangyou need to make sure you test and test and test13Rasmus to the Rescue!https://github.com/rlerdorf/php7dev/blob/master/README.md tl;drinstall virtualboxinstall vagrantgit clone https://github.com/rlerdorf/php7dev.gitcd php7devvagrant upvagrant sshsudo newphp 56 debug zts

There, now weve switched to using a precompiled debug zts version of php 5.6WOOHOO

There arent (yet) other box versions/providers available this is ongoing work (more on gophp7-ext later)14How to Play alonggit clone https://github.com/auroraeosrose/php-extensions-code.gitgit clone git://github.com/auroraeosrose/php-extensions-code.git git checkout scaffoldingbookmark lxr.php.net you will be using it a lotphpize./configuremakemake test

Im 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 arent necessarily complicated extensions but they do follow PHP CS, have all the copy and paste you need and do compile (I think)15Lets write an extension!But wait theres more.So the part were all excited about lets write an extension! Ill just get some code and then you copy and compile it and then16Set up compile environmentWrite a module definitionLearn about the PHP lifecycleLearn about zvalsAdd functions???Profit!

Every Other Extensions TalkIf youve 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 dont 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 ;)17DOIN IT RONG

In addition this is the absolutely wrong way to approach writing PHP extensions!

Actually its 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 youve designed the database and created the schema? do you create the schema before you have wireframes? just because were making a product in a different language doesnt mean the rules change18Which do YOU want?

this is why you really need to think about what youre 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 naturePlease be the solution, not the problemWe need no more painful APIs in PHP

19Do something you cant do in userlandUtilize a C libraryMake slow code faster

Maybe you should just use ffi!Step 1. Why?so these are the reasons youd 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 arent other re