PHP UK Conference 2013

Extensiones para PHP

Sara Golemon  · 
PHP

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

hi I'm Sarah Goldman I'm going to be talking about PHP extensions today I'm going to go really fast because there's a lot of content and there's only 45 minutes to do it in if you have questions I do just go ahead and throw them up we can

deal with them when we get there otherwise I'm going to not waste time that I'm going to go ahead and get started it's a quick overview of what we're going to talk about just going to cover cover some of the basics throw pieces in start building

things up like a recipe and we'll we'll just touch on a few of the sort of high-level things about accentuating so before you can actually start building an extension which you may want to do because you have some custom library that you want to wrap

or you have some bit of code that you want to just wrap really tightly and run really fast so you want to write it native C code before you do any of that you're going to actually have to put together an environment to do that with some of these things

are the really easy stuff like I need GCC make you know libtool auto comp things like that really basic stuff the one thing that you're going to need specifically for PHP is PHP development headers and a special build of PHP that allows you to catch things

that you won't catch with a normal bill for those development headers usually get those straight out of a distribution package you might get installed PHP developer it happens to be but that's still not going to give you the debug build which technically

is optional but again i really recommend that you and compile with these two extra options enable debug and enable maintainer ztf those will ensure that as you build its warning you hey this might work for your environment but it's not going to work in

Apache two threaded or it might work on a single request but you're going to be leaking memory and you'll run out very quickly you'll have to restart your server every five hours and nobody wants to do that so to get their environment setup just

run configure like you normally would on PHP for all your other options on there with live XML with P do whatever else and just toss those two on there build it up it'll be all set there is sort of a really basic extension this extension does one thing

it exposes a function called hello and that function says hello world see everybody's favorite contrived example right let's look at some of the pieces that are in here this module entry this is how PHP knows what your extension is going to do this

is sort of the starting point and the macro down here this is sort of cookie cutter this tells then how to find your module so the PHP knows what to do with it we have a list of function function and trees one entry per function that you're going to expose

and I'll show this list kind of grow over time that function table is going to point to your actual function implementations you've got macros everywhere so that you don't have to define out what would it be voids if underscore hello and that whole

bunch of parameters that mean nothing to you and some basic header glue and a configuration file over there this config file is your general auto comp and forcing text anything that you do in auto conçu can do here but of course there are some specific macros

that are for PHP development only such as new extension this is saying hey I want an extension called hello here's the files it needs to build in this case just one and I'm going to enable that through the switch enable hello building it like is like

building any other pickle extension right you go to our module directory PHP is that's going to take your config in for build a configure script out of it configure that's going to run that configure script to create the make file make got your module

load it up with HP the dash end means don't load my usual PHP I and I because we want to get confused by other modules doing things override the module directory to our it got built load it up and run it yay we've got an extension and it only took

a couple of minutes to actually have a nice day obviously we need to do some more things with these extensions accepting parameters is kind of a funky thing we don't modify the prototype to the function we have standard prototype for all PHP functions

what we do is we go into the stack and we say pull those arguments off the stack and parse them into specific types for what we want to do in this case we're giving the hello function a single parameter called name that's going to be a string and we're

going to load it into this car star and int that we pass in by reference and then from there it's just a normal see variable we can say hello Bob I've also added one more thing to this function entry down here this hello Argan phone this references

more macros this is for reflection if you want to ask the extension what sort of functions it has you can say dash dash RF function name and a list all the functions with their parameters handy little thing it only knows what those parameters are if you define

them up here in this structure and I'm going to knock my laptop off of this podium here now I used s in that example there are a couple other types that you can pull off the parameter sec ultimately every single one of these types is just this one down

here it's called as evil or dead bowel if you prefer this is sort of the unifying type for all data in PHP all these other types are just going to say hey make sure it's the right you know underlying type but she's into a bowl or whatever it's

not try to convert it if you're allowed to and then drop it into the appropriate fee structure I'm not going to be labor all of these they the uses of them should hopefully be pretty obvious off the bat if not you can raise your hand and ask about

them at this point let's use a few of those we're going to extend our example a little bit further now instead of just asking for string we're asking for a required strength and then an optional string that's what this pipe parameter means

that it go over that in detail there we have three modifiers to these types specifiers pipe can only occur once it means everything to the left is required everything to the right is optional if you don't pass one that's optional the original see type

variable is left untouched so in this example the second string is optional so we have to give it a default value if I don't have something for the second value such as in these this default lap value is going to be left alone and that's what we'll

have when we get down to actually using it of course if you do pass something but it is similarly we can add this bang on to our type specifier that allows us to actually do pass this value but pass it is null and then it'll still have the same effect

is not passing the value it'll leave it whatever the default is userspace can't do this you might notice there is a proposal up for 5.6 to allow default parameters but that hasn't been accepted so don't look for it one other modifier and that

is the forward slash this one gets a little tricky so let's pretend this forward slash isn't here at the moment and we're running this code we're pulling a string off the stack storing it a name or print out just hello by itself we're going

to write out the string and we've also got the stir to upper line in here right what the PHP start to up our function does is it won't actually allocate a new string it'll just overwrite the string that we pass in moving all the characters to uppercase

or lowercase if that's when you're calling the trouble with that is that without this whatever the user passed in is going to get modified because we don't actually copy the value as it's coming in and there's no copy on write protection

once you're inside C space by saying forward slash we're saying separate this value that is make a full copy of it so that we can do whatever the hell we want to its internals and it's going to get thrown away at the end of the function so it's

[ ... ]

Nota: se han omitido las otras 4.008 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.