 I'm giving a talk as it's an introduction into PHP extensions When I was yesterday talking to somebody they thought I was going to show you lots of different PHP extensions Which is not the case. I'm going to talk a little bit about Why you move what why you might want to write an extension and then show a little bit on how you actually end up doing this I'm a little bit about myself. I'm Derek. Hello. Good morning Good morning. Just making sure you're awake at I Work from longer to be on the PHP driver from longer to be As Matt already said I wrote X debug PHP's daytime support and a whole bunch of other things. I like maps. I like beer I like whisky I don't think I have any examples of either of those in my slides as I usually have There's any questions will keep those to the end because otherwise you need to keep turning off the lights and stuff like that And I'm more than happy to hear your later questions and comments either on Twitter or by email Or and joined in of course, but I'll show the other contact details on the last slide All right, so what are we going to talk about this morning? Well, we're going to have a look at what our extensions Why would you want to write one and how do you write an extension now? The last point really needs about a week's time. I have 50 minutes So don't expect that you'll be a fully-fledged PHP extension developer by the end of this presentation. It's just not enough time for this to do in 50 minutes Or even a day really All right, so what are extensions? Well extensions and PHP bring you additional functions and classes So if you just look at PHP raw The only thing it really implements is the syntax. It won't have things functions like stln or any of the array functions They all implement those extensions now all those extensions are written in C Some of them like for example the date time extension or the standard extension which implements all the array functions and string functions stuff like that They are actually part of PHP and are very difficult to separate from it But technically they're developed as a extension and some people have tried in the past When they wanted to run PHP on embedded systems actually not load those by change making things to the code so These are although these are extensions. There's of course other extensions that you load Did you sometimes load into PHP? It's very common for Linux distribution that all the extra extension and part of the PHP core for example you load True shared objects those are extensions that are compelled separately from PHP But loaded into the same process to your extension equals lines in PHP. I know Some of them are bundled with PHP. You have for example, my school is bundled with PHP Some are distributed to Peckle, which is the PHP extension code library That there's a background in I mean, I don't Sure originally meant something else, but We'll just make something up as we go, right? For example like the MongoDB extension you can install by by using Peckle install extension name And there's also a whole bunch of extensions that are not available through Peckle So you need to go find them on the internet on clone GitHub repositories and whatever things people come up with sometimes So there's different class of extensions different ways of distributing them And but are all written in C with a very old exception of an extension written in C++ here and there All right, so why would you want to write one? I mean, it's a lot harder to write C code and that's the right PHP code especially when you need to think about debugging as well as memory management and stuff like that, so When when starting to think why would I want to write an extension? Well, there's a few reasons why you might want to do that So the first one is if you want to wrap an existing library Not everything can actually be implemented in PHP code directly or sometimes for speed So let's have a look at each of these categories. So the first one is the wrapping libraries on right so most database drivers, there's always a Library system level library available that implements all the complicated connection management Query translation and stuff like that. So for MongoDB, that's LibMonger C for MySQL, that's LibMySQL as an example here Immort processing libraries much more easy to do because These libraries already exist rewriting them in PHP is a bit silly. Although some people do that You can you can write 3d rendering ray traces in PHP if you really want to do that It's not going to be very fast, but you can and so yeah Just wrapping these libraries that is one of the one of the use case and the one that most people actually tend to do because I already have some functionality they now want to make available in PHP and Writing this very thin layer in between a library and PHP isn't actually that difficult to do So the other thing is there's not everything is possible doing in user land and with user land I mean writing things in PHP itself like writing PHP scripts So if you need to persist data in between requests, you can't really do that in PHP user land You can of course store things in shared memory like PHP data structure and stuff like that But you cannot do that for connections See see your system level connections to your database for example You can't really model those in PHP user land So if you want to have persistent connections, you're required to that in extensions and in extension There's different ways and tricks of how you can do These these persistent connections and then of course as a whole bunch of extensions that need to look so deep into PHP internals That yeah, you just can't do that in PHP user land So the things is this VLD which is a tool to to see what goes on in the hood in PHP or XD bug The things that XD bug does you can definitely not do in PHP user lands and the same thing with PHP opcache, right? It is a it's a low-level System thing kind of stuff that that stores things in shared memory for you and that's something you can do in PHP And so those things you have to write an extension for and then the third one is speed I mean PHP is a very fast language, especially in PHP 7 But see is still going to be faster and there's no way around us, right? So it which means that algorithmic implementations in C Often outperform code on PHP that should be Should be quite obvious really right? all right, so On the other side, why do you not want to implement something extension? I think my abstract at some point said and I will tell you why not to implement all of all of symphony in extension, right? Because it takes a lot more time to write extensions Then it takes to write PHP code C is a harder language you need to deal with pointers You need to deal with memory management You to make sure you clean up your memory that you don't reuse memory that you've already freed none of those problems exist in PHP The same on the same side debugging things in C extensions is harder than doing this in PHP as well I mean the debugging tools are just as good But it's because the whole concept is more complex the debugging is more complex as well And because of that the maintenance it takes a lot more time to make the maintainers extensions, right? So It makes sense that if you have something in a big PHP framework or let me take an example that actually is a real example The trick templating language When this is written for seven eight years ago might be a bit old out on the time frame, but about seven eight years ago is People have wrote a Fabian potentiary. I think he reached out to me So well, we've used profilers and figured out everything, but there's this one function there's one function that is just really slow and we can't optimize this and You'd ask well, can you see where our writing extension for this actually makes sense? So there's now a trick see extension that implements just this one single function Because that's the only thing that made sense to write in in C code because it was this one thing It couldn't be optimized in PHP anymore, but it still was taking a lot of time So doing that and see makes a lot of sense and of course if you use trick You can now choose to install this extension or count or choose not to install it So implementing very specific parts of an application or framework makes a lot of sense sometimes Do an alph symphony or alph take it's going to take you years and years and years and you're not going to be very happy afterwards. I Can tell I've tried. All right, so let's talk about Belgium Oh The maps came back after I'd forgotten so Belgium, right if you want to draw the outline of Belgium You have quite a lot of lines here, right? Of course if you zoom out, you don't really care about all the weird squiggly lines of border between the Netherlands and Belgium For example at the top so when you zoom out drawing the board outline the border outline of Belgium It's taking a lot of time right especially if you want to look for all the countries in the world I mean the browser is going to take a lot of time drawing all those things with true JavaScript lines and canvas stuff and whatever So having an algorithm that optimizes the outline of polygons Actually makes a lot of sense. You need to do something like that. Otherwise. You just won't be able to show things in a browser anymore So this is actually thing I was looking at for some hobby project at some point and I realized well I need to do this better I mean I need to simplify those lines in order to be able to draw all the things I want on the map All right, so Yeah, when you get to Belgium polygon I mean I can't scroll down around but it's a very very long list of coordinates, right? It's like 1200 lines or something like that And there's of course algorithms to simplify these polygons into something so It's called a ram at Douglas Pucker if that's how you pronounce that. I don't know something like that, which is now a recursive algorithm that Simplifies polygons by looking at the full line Determining like in something they call an epsilon, which is that it's difficult to see in the contrasts here, but it's the How do you say that the light blue Box it draws around the lines in the algorithm. So that is the epsilon the way how this works if you look at the line You draw the line between the outside points You draw the epsilon box around it and all the points that fall out of the box are considered to stay in the in the end result of the of the algorithm so This is a recursive algorithm and then comes up with a simplified polygon now if I did that for Belgium 100 times with an epsilon of 0.01, which is only a small Small simplification doing that in the PHP function takes about 45 seconds Doing this in C. It took me a third of a second, which is a whole lot faster. Alright, so we're going warp speeds Need to get the geek jokes in somehow And so yes, it is so much faster, right and because this is an an algorithmally complex thing to run Implementing these little things in C makes a lot of sense. So let's have a look at how we're actually doing that So what makes up an extension and PHP now? This is a mostly it is conventions Not all the extensions follow all of these things exactly, but most of the most extensions will do So there is a config m4 and a config W32 file. These are the Files that provide build instructions to PHP's build system and for is a really ridiculously annoying macro language We've just gone through Reimplementing this for the MongoDB driver and because we have so many libraries that we need to depend on doing that is not easy It is a really really gross language to use Config.w32, which is this Windows equivalent is all done in JavaScript ish Which is a lot easier but then on Windows you don't really need to do much configuration because you can assume Things being there a lot more than on a Linux or Unix base system, right? Because config m4 is for every unit she can think of and max whereas the W32 is just for Windows making that a lot easier All right, so we'll look at all those files in a moment You have a PHP underscore extension name dot h file Which is the header files and provides some glue some help to PHP to be able to load things and stuff like that You have extension name dot C which implements The extension definition from clean implementation is basically your implementation You can choose to have of course multiple files So if you have an algorithm already as a C function, you often have separate function to implement specific things If you get a more complex extension For example for MongoDB we will have a C file for every class that we have in the extension just to make it more manageable XD book has like 40 or 50 different files there But many many extensions just have this extension dot C file this php underscore extension dot H file the config m4 file And then of course it is a good thing to write tests for extensions, right and tests are written in a dot php t format which is a very simple way of providing the PHP script to run and then Comparing that to the output that you expect again. I'll show you that in a moment All right, so the smallest extension you can probably get away with is about this size I know you can't read this. I can't read it on the slide either But it is about what is it's hundred lines of code and total and this is the most minimal extension that Implements a function that is called hello world and shows hello world on the screen. I mean you need to start with something, right? Um So let's have a look at all of those files that make up this stuff So the first thing to look at because and because this is such a simple extension that we're going to look at is All very small. So this config m4 phone They often start with like a little comment saying this is the config m4 for for the extension with the extension name In this case the name of the extension is called geospatial Which is an actual extension that I've written that implements this algorithm and a whole bunch of other things And then all the people have contributed to this extension so it implements a lots of geospatial related algorithms like coordinate Transversions a polygon simplification that I'm looking at here now and a whole bunch of other things So the next few lines you get in the config m4 file is you need to define your configure option flanks So in this case we're saying PHP arg enable geospatial in the name of your extension Then you get a description This is description that shows when you would configure the extension by typing Dot slash configure on the command line. It will then check whether this argument has been passed Show you the whether to enable geospatial support in its output And if you would do configure dash dash help you'll get the enable geospatial and enable geospatial support That is what those two lines really do it is convention that enable issues for extensions that do not Depend on external libraries, and it is convention to use PHP in our arc with WITH for extensions that depend on external libraries why that convention exists. I don't know, but that's just how it is Yeah, and this is this m4 kind of language to macro stuff and Then after all your options all your libraries have been checked for example Then you need to tell PHP build system to do a few things as well So the build instructions here say well if we had passed in the flag and it isn't no Because you can do enable geospatial equals no Why you would want to do that? I don't know but you can And you need to test for that I mean this does not make a lot of sense to do for standalone extensions But it makes a lot of sense if you have extensions that are part of the PHP core, right? And then you might want to choose whether you enable something or not this being external extension Not enabling is silly because why would you bother trying to compile it in the first place? So but yeah, it's convention. It needs to be there and then with the PHP new extension macro You define well my the name of my extension is geospatial I have the following C files that you need to compile you have a comma and then you have the dollar X shared Which you just need to do I Can't explain you why either so But it needs to be there. Otherwise it doesn't work Yeah, the magical of m4. That's what I'm saying All right, the W32 file is very simple very similar But it's a done in Java script There's arc enable and see php arc enable the name is the same you need to specify the default argument here in this case or The reverse negative whatever the no is there. I don't know exactly why to be honest And a set of PHP new extension you just use extension because this is built isn't being written After PHP's build system has been rewritten a few times So in PHP have PHP new extension, which is of course a newer version of PHP extension but nobody uses the old one anymore and Again, you have to specify the C files and you don't have to do the silly X shared stuff because this is a lot more Stainer to use to be honest alright, so I've shown you to come again for fun, but of course we haven't really spoken about how you compile external extensions There's usually three steps if you speckle install extension name it will run those steps for you So you don't have to think about it, but if you would clone extension from get for example You need to run PHP eyes PHP eyes is a tool that comes with the PHP dev package most of the time Which is a little script that proof prepares PHP's build system with the information in the extension So it will look at the config and for fun if there's other files It will do things with that just to prepare them so that when you then run configure it Goes through the script and does things with it So PHP eyes is equivalent to if you've ever compiled PHP itself if you do dot slash build comf That is what PHP eyes is for extensions and it it creates this Configure script that you then need to run To check all the things that are available in the system or not check for your your arguments And then that creates a make phone and a make fall can then be run by make to build the extension So those three steps are there and you need all the time What I do for most of my extension I actually created a little script which is called rebuild as age which is all of these steps for me and a few other things so it sets on compiler flag that I like using for For giving me more with warnings and errors when developing extensions. So, yeah It would be nice that if PHP eyes Would have this rebuild thing in there or something like that. I don't know make sense All right, so let's look at the the other files that we have so the header phone Defines a few things about your extension They always start with this guard. It's a thing in C That makes sure that if you include end up including ahead of all multiple times you don't actually do What's you don't actually include what is in the header fall multiple times? Because if you end up defining things in a header form Defining them twice you get it you get an error or a warning out of there So the first two lines here the if not death if not defined PHP underscore geospatial.h So basically everything in the whole file gets excluded if PHP geospatial.h is already defined And then the next line defines it making sure that you can really it really only Does the instructions in this fall at maximum once? Then you have a version string It is also convention that you use PHP underscore extension name underscore version if you don't do that pack Packaging tools will actually warn about that You get your model definition that is something that PHP requires to be able to load a shared extension That needs to be there. It needs to be the exact same format with the exact same names The word geospatial and geospatial model entry needs to match the name of your extension needs to match the name of your shared objects Then often you get some module Inutilize Inutilize the declarations. These are definitions for PHP M in its which stands for module in it Which is a function that gets run when an extension gets loaded. You can do initialization things in there, for example I'm info is the function that's going to be called when you do PHP info So an extension can define their own table of information to show in there As you can see if you know see well enough, they are just forward declarations They don't actually implement anything in a head of all in in C. You don't implement anything ahead of all and Then you have some all the forward declarations Basically defining all the functions that you have in your extension in this case We have RDP underscore simplify, which is the name of the function. We're going to have a look at it implementing Make sense Yeah, nobody's fallen asleep, but I hear no snoring. That's always good. All right, so Now we have done the header file the main thing is of course all happens in the C file and in the C file They usually follow the same order of things, but it isn't strictly necessary. It is a bit of a preference where you put all the All the the information to tell the extension what the name of functions are and where to find the implementation at the bottom Not everybody does that. Some people put it at the top. What can I say? It's preference But what you always have at the top is your headings inclusions that these headers define things of the PHP API and Almost all of the time You need PHP of the age included because without it you won't be able to get anything done PHP I now you need to include if you have in a like PHP I now settings if you have a M info function to get things into PHP in file put you need to include X standard info dot H You also always include you had a fault for your extension, of course, and then if you make use of exceptions Normal exceptions are Zen slash exceptions if use SPL exceptions is X SPL SPL under square exceptions and then For many other things that you might use from the PHP API You need to have a specific header include fall for array functions for string functions They're all different files that you need to include and it depends of course what you make use of in a PHP API which once you include All right after the header files you get argument information definitions deep This is information that you provide that you will see when you use reflection And it also gets used for argument parsing argument parcel get back to in a moment So P is end begin arc info X You give a name the name is usually function name and then underscore arcs again convention Then you get a few flags in there the last one being the most important saying This is the amount of arguments that are required to be pulsing to this function So this one says it has two arguments and both are required to be pulsing and then you define the names of these Of the arguments and this is only the names that show up when you use reflection nothing else is really done with that If you have arrays or objects, there's different version. There's zent arc info array and zent arc info Object so you get to define a class name with and the names of the classes are actually being checked when you parsing arguments So it doesn't it does do some things with argument parsing, but not everything that you might want to them and Then you need to define a list of functions and the list of functions as well That's what I say is what it is. It's a list of functions And there is a macro called PHP underscore FE which stands for PHP function entry you link the name In this case the name of the function that we're implementing RDP underscore simplify It's also going to be the name of the implementation file Sorry, not the name of the implementation function in the same C file Sometimes you might want to alias then there's macros for that if you want to methods Define on classes. That's Pete. There's PHP underscore ME. There's M alias There's a whole bunch of these different macros doing slightly different things That's more complicated if you want to the namespaces again But there's a whole bunch of these in this case. We're keeping it simple So we just use PHP FE as PHP function entry and now we still haven't gotten to actually implementing anything and we won't be there for a moment So in order for PHP to know where all your implementations live You need to have this module extension definition. This defines the version number the name Which initialization and destruction methods are being used. So we have M in it here and M info here The geospatial version you need to do I don't know why I still have the if Zen module API number is larger than the number because this is Stuff that has been added to PHP API as you can see on the number in 2001. So Probably not necessary to keep it anymore Because there's no way this extension will compile with PHP 4. I can guarantee you that that won't work All right, and then the last bits the last few lines are actually necessary for to make sure that PHP can find the the function definitions when it loads things into Into PHP itself and this is done with a guard. It's it's the compile the DLG geospatial So it would only define its function When it's being loaded as a shared object because when it's built When you choose to compile an extension with PHP's normal build system You can't define this method because then things don't work. So that's why there's a guard around it all right And then you get some auxiliary in it and info section. So I'm in it Yeah, it's a place where you can for example register constants If you have to initialize a library that you make use of goes into M in it as well and in it is being run once per Per process so on to PHP FPM or PHP CLI starts up. It will call this M in it function once Sometimes it is necessary to initialization per request For that you have our in it In our in it you can do a little bit more you can allocate a few things that you can't do an M in it And that tends to be you allocate things in there you initialize things in there for For just a single request and you're going to have in most cases an R shutdown Which is the opposite of the initialization because if you initialize things It is very likely that you might have to an initialize things as well in this case I don't have an M shutdown because the registration of a constant You don't have to undo when PHP exits because it's exiting anyway, so it's what is the point there, right? And then you have M info which is kind of funny because you use C functions to build HTML tables Yeah, there's no templating or anything in there really it's kind of it's quite a quirky bit of works and It is it's recommended that you show at least the name of the extension in there And then which version it is so that you when you see PHP info you can see which version of the extension is run Which is my my only row of information basically in it all right, so So far what we have seen is the implementation. It's all boilerplate every extension needs all of this and it doesn't particularly Interesting to look at and write on what the interesting things are is of course the function itself Right, this is where you do all the useful works So there's usually four stages for every function that you use that you implement You need to look at all the arguments that are being passed in from PHP scripts user land kind of things Libraries often needs this information in a slightly different format So you need to convert the data that comes into PHP into the right format Usually different see data structures and things like that. I'll show you in a moment Then you need to run the stuff. That's a library implements and then From that you need to convert that to the right data structure so that you can return it back To the use that calls the PHP function Anybody wants to guess what this function is No, no physics geeks here You go have to look this up later Homework, that's why I call all right. So parsing the the the input parameters So I will slowly go through a whole implementation of a single function now and I'm doing this in sections And as I said the first one is parsing input part parameters. So in PHP every bit of data that is used is usually something we call a Z fault or if you're an American as evil I Prefer Z fault a Z fault is a data structure that wraps around a value as well as a type Because PHP is a weekly type language PHP for every variable needs to keep the type as well as a value In order to do information things with it later, right? So that is what the Z fault is a double is see See way of calling a floating point number. So you can also use float but has less precision In PHP your floating point is always a double. So because it's always a double in C You need to use a double here So for parsing input parameters, there's a PHP API function called Zen parse parameters Which accepts a bunch of arguments here? So it first has Zendem arc, which is a marker that tells you how many arguments are being passed in in this case We as because you remember in the arc info we define that it has to be to so that will always be to or nothing Nothing if because the extent that the function won't get called The T is a MLS CC thing. I'm going to skip over because in p3 7. That's no longer necessary And then the argument that you then get is the one a double quotes the one that says ZD is your it's your definition of what types of Arguments are being passed in and depending on the letter you get as Subsequent arguments to Zen parse parameters You need to provide a definition or a reference to the C Data type that you have defined beforehand. So the letter Z means I'm going to see a Z fault We don't really know what kind of data this is and we're going to have to look at later But it's the right type and do something with it Epsilon is this like this This is argument to the function that defines its smoothness basically This is a double and a double we specify with the letter D If you get your letters with your data types wrong, it will crash if you need to get us right Luckily, there's a nice documentation file one of the futon that is actually documented on the PHP API to be honest Which links your letters which would they see data types that you need So then perhaps parameters requires a format letter every type there a match with a C type and also The The the data that you get in here gets freed up at the end of calling the function Which is important if you have things like a string and C You are not allowed to free that string because if you free that string P2P tries to do that for you again, and it's already freed and then it crashes Basically everything you do wrong crashes. So you need to be a bit careful that All right, so yeah, as I said, although we get in the first argument as a Z fault We still want this to be an array now. There is a specific letter for an array. Why I haven't used that. I can't quite tell you I think with the intention of later also supporting objects in there and there is Well, there wasn't when I wrote his extension a letter that would accept both an array or an object at the same time that exists now So it's a little bit old code sometimes but what we can do with What I've done in the last three lines of implementation here the Z underscore type Returns the type of the Z fault variable and then you can compare that with is underscore data type in this case an array Well, so what I'm saying here if points array is not an array. I'm going to return from the function so Returning from a function in in phpc implementation stuff and means you the function returns null the Way how you return variables from php implemented from functions like we've implementing here is not by using return in C, which is kind of weird, but that's how it is All right, so what is the Z fault thing? I've already touched on that a little bit So this is a PHP representation of a value The implementation of that has changed significantly between php4 php5 and php7 But most of that has been abstracted for you. You don't have to deal with very much So as I said, it contains a type which you can access through Z type P And you can also access to very the value in there by using Z underscore Short name for data type Val underscore P. So there is our role for getting the array information out of there There is B valve for the Boolean value. That's L valve for an integer number. There's D valve for double There's still Val for a string There's still L for the length of a string and so on and so on. Yeah, I know those acronyms are very interesting to pronounce but those all exist and If you don't know beforehand What the data type is you really need to check it with Z type P before you access information Because if you would try to access the array information in a variable, but it's actually a double It's going to crash And I said everything is going to crash if you did run or the chance of things crashing Increases, let me put it up. So as I said, there's a whole bunch of these letters This is not an exhaustive list because it would fit on the slide But there are the important ones a for array B for Boolean if you want to accept objects you can use an uppercase O and And Unlike a normal argument it exactly it Requires two extra argument to Zen parse parameters So the first one is going to be a Z fault which is going to be handled to your object And then the second one is something called a Zen class entry Which basically is the definition of your class the most important thing in this case in there It is going to contain the name of the class So with the uppercase O you can actually instruct Zen parse parameters to already check whether a object that you pause in is Is of the correct class and if it isn't you get your standard PHP? argument arguments wrong error back so Zen parse parameters actually does quite a lot of these things for you so that you don't have to check the right types and stuff like that All right As I said after accepting the arguments We now need to convert it to something that the library can do something with so what I've done I have implemented this RDP simplify function as a C function sort of in an external files to Simulate this is actually a library. It isn't really the case because it's built with the extension, but Illustrates the example better and in there I have a implemented function which called geo hash table to array which converts my Z fault in points array to a data structure that my See implementation of the RDP simplify function in a stance And it looks something like this. I just want to show you this I don't want to go fully over this because it's going to depend very much on things But there's interesting things are in there like PHP has specific macros Like Zen hash for each well to iterate over an array for you Which which works very similar as a for each in PHP itself So there's for each valve that will only do you the value? There's also for each index well or IDX well if you want to get both a numerical index as well as the value And there's also of course one for having associative rays and stuff like that There there are a new addition in PHP 7 that makes doing these kinds of loops very much easier than a previous iteration of it So basically what I'm doing here with before each well I'm looping over the array and each of the Elements in the array is a coordinate pairs a longitude latitude pair And that's what I'm parsing with parsed point pair I'm parsing this element It will also check for the correct types and then returning them to the information in long and that's All that's to a C base array, which is the TMP variable in here I set the status to one for each of those point and the status one for the algorithm means this is a point We're going to keep so once the algorithm runs it will set for some point the state is to zero I'll get to that in a moment also important because we created a data structure if something goes wrong We need to free the data structure if you don't free the data structure you got something called a memleague and That is okay once but not if this runs a hundred thousand times and then your PHP process runs out of memory It's likely means it's going to crash All right, so the main thing is of course we running the algorithm now. I'm not I'm going to show you a little bit the How close is C implementation of RDP simplify would be to the PHP function. So that's the next few slides, okay? So we have RDP and PHP It is kind of complex, but I know there's like a few more lines. It doesn't fit on the slide It doesn't matter, but just compare that to this to the info the one and see right Can you see what how close they are in? PHP you of course you don't have to define a data type for first point last point and index in C you see why you need to define those as C data types because and see you need to declare variables if You look at the ends minus start being smaller than two in PHP. We have dollar signs in front of the end and the start It's very quite close doing like Algorithm kind of things in C and PHP isn't very far away from each other The problems are going to be mostly memory management that you need to take that is probably one of the bigger things that is different Between writing something and see then it is in PHP. I won't go through the whole algorithm Oh, I have the slides put online already so you can go through it later or find the extension itself and see what all The kinds of cool things we've implemented in there All right, so I've said so well We pass the argument meant we convert them to something that the library uses we execute the library function And then of course we need to return the information back to the person that's called a function or the user in this case So in this case what we're doing is we need to tell that a return value is going to be an array and there is a magic magic No, my laser pointed out is a magic variable Return called the return underscore value that you do not declare yourself If you see the the start of an implementation in this case the top line of my example here says PHP underscore function Which is a macro. It is a macro that does do a few extra things With the function name and one of the things is make sure that you get those magic variables like return value or it provides the information that Zen parse parameters uses you don't need to do anything about it yourself But it is a variable that you of course you use for written an information back to the users And because it's an array we need to call array in a talent What you should not do is allocate this by By by allocating a Z fault for it because it's already allocated for you and if you do that It's likely that things will crash. I know from experience in this case I know I keep saying that a lot in any case Was a library turned is a data structure that is stored in points It's the same array that we pass into the library function But now it has modified the status flag for each of those points So what we're now doing is we're looping over this array Which we know how many elements it has because that's part of the data structure in count For each of those pairs what we create is we create a array we do use array in it to create to not allocate to create in an already Available variable called that fall pair second line of the example We we tell PHP to turn that into an array variable so that we can add elements to it So the the four lines in the middle basically creates an array then adds to two Coordinates of the latitude and longitude which are done in X and Y in this case and then it adds the newly created Structure with a coordinate pair in it to the array in return value So it's odd index sorry out next index Z fault Basically is PHP speak for odd a element to the end of the array if you have an associative array There's a different function for that If you have something else than the Z fault if you have a double there's art index art next index double and so on and so on If you already have a array key There's a function called art Index Z fault where you then have to specify the numerical key and so on and so on so there's a whole bunch of these functions Depending on what sort of data type you add and whether you have a key already or not and stuff like that And again important to know that once we are done By adding all these things to the return value is to free your data structure Because if you don't free the data structure that you've allocated PHP won't do it for you And then you have a memory link and luckily there's tools for that too for you to find out those things exist All right, and that's what we're done Except that there's also different ways of returning information, right? I mentioned before that if you just call return with a semicolon PHP ends up returning null to use a land and there's a different ways how you can actually return information like what I've done here is You see I actually don't end up returning anything. There's no return statement But what I have done is I've done this array in it with return value So I'm basically manipulating the already allocated return value with information. I want to do If it's an array, there's no macros for this if it's a sink simple values There is something like that. So taking example from the MongoDB driver We want to make sure that the cursor is alive So we use return ball and what return ball does is add this information to underscore return value and then immediately return from the function Which is not something you always want to do because as I said you need to clean up all your memory Allocated memory structures are the things you've allocated a memory you need to free But if he's one of the return underscore macros It would return immediately from the function without you having a chance to clean up and for these situations there is another set of macros and they're called rats fell underscore and then exactly the same as before So that you can then still clean up The data structure that you've allocated So yeah, so you can return immediately or you can just set the return value You can then later change the return value again as well if you want to do that And I just have a just a quick thing about PHP 5 versus PHP 7 as an extension developers It is sometimes a little bit annoying that you have to take care of both of them Of course people writing extensions now They're not going to have to take care about PHP 5 anymore because none of you should be using this Yeah, right Yes, my PHP 7 spill. All right, so that's returning as I said before many times memory management is very important PHP's API has some assistant things with you there Sometimes if you have a Z fault you need to If you need to use a Z fault that you have created in one function used in all the functions You need to use a Z odd ref that's like a Bumps a flag in the Z fault to make sure that if PHP wants to destruct it It only lowers down the counter and it would only end up freeing it if the counter hit zero So everything gets allocated with the counter of one if you use odd ref it bumps that's to two Then then the function ends and parse parameters functionality Decreases the number it doesn't as long as it doesn't get returned to zero it It won't free it so that's to keep things alive and safe in the same request Of course if you do that at one point at some other point you're going to have to decrease the number Otherwise you get a memory leak, which is not cool and an annoyingly that is not called Z Dell ref Why? PHP All right So and then the last thing of course you need to write tests right and the test go in the test directory They're in a PHP T format and you run them that make tests a Simple test file is not much more than a simple file with few sections in there So the section that I've used in here are the ones in red So test defines the name of your test the one that shows up if you type make test you have the file that is a PHP script That's what it is. It's no more. No less and Then the expect section is what you expect this PHP script to output to standard output There's no such thing as you have assertions like you have in PHP unit for example It is very simple Comparising the output of a script to what it does and this is something sometimes important because of course things in Britain and sea can crash on times and If all of this would run in the same requests if one test would fail or crash then it would stop running all of the tests, so What make test actually does it spins up a PHP process for every test and by doing that having to Load things like PHP unit in there for every single process is going to make things so slow to make it unusable Also when PHP T was invented PHP unit wasn't invented yet. So it's all technology here There's a few other things you can do instead of expect F You can also use X sorry in instead of expects you can also use aspect X or that's difficult to pronounce expect F Where you can then use things like dollar D to match a number or dollar as no dollar Sorry percent S or percent D to match a string to match a number and all the other data type You can also put regular expressions in there if you want to do that But yeah, it's just a simple PHP script. So they're very simple to write which is a good thing really You