 Okay, thank you everyone for coming. This is PHP 7, the new new PHP, but first this word from our sponsor Awkward is the digital experience man. I'm just kidding So so I've known Larry for about 10 years now or something like that a really long time Yeah, coming up on 11 We first met at Drupalcon Sunnyvale and it was interesting because back then like Drupal.org didn't have funny you know user pictures or any of that we were all just blue nicknames and So I had in my mind a picture of Larry based on his online interactions It's like this burly muscle-bound guy with like long greasy hair Like I don't know and then I met him. He's like this scrawny nerd with like orange hair So I thought that was pretty cool. So he's been defying expectations ever since but what I really like about Larry is He really goes after like huge amazingly insurmountable problems You know everything from trying to make every hosting company migrate to PHP 5 back in the day to You know making Drupal's database abstraction layer You know something other than raw sequel right it and written everywhere and on all the way up to you know Bringing Drupal off of the Drupal Island and incorporating best practices from libraries everywhere else and Larry's kind of taken Each of these things by the horns and wrestling with the ground and really brought the project forward So I really wanted to say thanks for that. So what you're saying is I I'm a fool who tackles things without realizing just how big they are pretty much. Yeah, but I'm one of those two So we yeah fist bump. No, anyway Yeah, that's right. What else did I want to say about you? Oh? You recently started a job with a platform where you are the I'm gonna have to look at this Director of run times integrations and services. Does that intentionally an acronym that spells trees? It was very close when we were coming up with it and decided to just put it the rest of the way Okay, yes, I thought that was right. So you're working for platform.sh these days We can blame you for Drupal 8 being too classy with the object orientation things And you're also on the PHP framework and your ability group where you are the editor of the huggable interface All right, so what is PSR a compatible? All right, cool. So without further ado, I'm gonna hand it over to Larry Garfield to talk about the future of PHP Here we go For those of you who don't know her that's Angie Webchick Byron with our product owners Drupal 7 release maintainer and a gazillion other Pieces of awesome that I'm not gonna mention now because I could fill my entire session with that So now that that's over All right. Oh, we talked about me. Let's talk about PHP 7. That's why we're here, right? PHP 7 is You know it's new it was released last fall It's cool. It's wonderful What happened to PHP 6? Yes, I see PHP 6 actually eloped with Pearl 6 they moved to Vegas No, no, no Yeah, there actually was a PHP 6 at one point in development and its big feature was going to be Unicode everything and they never actually got that to work So the internals team ripped out the Unicode part and released the rest of what was PHP 6 as PHP 5.3. So we've been living in the PHP 6 era for the last five years and now we're up to the PHP 7 era So what's cool about PHP 7? Well, it's got a larger number on the end Besides that There's a lot new in PHP 7 something like 30 or 40 RFCs that passed for it With new feature improvements on July improvements under the hood I'm going to talk today about what I consider the 10 most important improvements for developers for people actually writing PHP code It's I am super excited about PHP 7. It's one of the most important releases of PHP in That decade I'd say it's the biggest change since PHP 5 5.0 and For you to leave here saying oh my god PHP 7. Yes, so let's see if we can pull this off Number one, let me ask you this question Do you trust your dock blocks? Who here trust their dock blocks? Wow, it's even fewer than I expected. I eat one. We got one person. Okay So let's have a look at this code. It's a nice Example code here. We have this method called get address Can you tell what get address returns? I? have no idea and Whatever it's returning. It's going to get passed to This distance between Function do we know that it's even right? Is that expecting the same thing? Or is it going to even work or is it going to fatal as soon as I object? Or at least I'm going to claim that it's an address object. I'm going to suggest that it might be an address object is That talk block accurate I Give it better than 50 percent odds But no more than that And of course who knows maybe the function is going to return null in some cases and then I try to use That address objects and get null and oh look my code blows up. That's lovely We need some way that we can definitively guarantee What we're going to get back from that method and Hey look We want to put this in the syntax so that the code will check it for us and now we can as Of PHP 7 we have return types You specify colon address this is going to return an address period always return types the number one new feature in PHP 7 I shouldn't say it that way the first feature major new feature in PHP 7 Every method every function can now optionally specify a single return type Syntax is colon and then whatever Just like you know you can type hints parameters They are not nullable Nullable means it's thing or null these are not nullable Which means if you type in that that method returns an address object and you try to return Anything that is not an address object PHP will complain to you Including null if you get back a value from calling that method you know Guaranteed you have an address object. Otherwise the code would have already exploded on you Let's look at this code. You probably all written code like this at some point Load something out of a repository get some object off it get another object off it what happens if you May have a null object Then you run this Method called a non-object. Lovely. Who has seen this at some point in their code and gone fag nab it Pretty much everybody. Yep. How do you fix this? Well? In PHP 5 you add a whole bunch of if statements around your code Correct. Yeah, it no, let's not do that if it's typed then You know you're gonna get back an address if you try to return a null then PHP's runtime itself will object and you don't have to handle it the code the engine is already going to handle that for you But if you can't return null on error conditions, then what do you do? Well, you could throw an exception Or you could also return an empty value object of some kind Which of these you want to do depends on your use case, but all this is making for more robust Debuggable code if you have a repository of some kinds I would recommend throwing an exception if you're asking for a user by ID But there is no such user the odds of the rest of the code coming after that working are pretty slim So just bail out exception. You know handle it like any other exception Because that's really yeah, that is an invalid argument If you have a value object of some kind I Would recommend creating a null object Lots of variable types have a natural empty equivalent Under for a string the empty version is an empty string For an array the empty version is an empty array and those are all typed safe Which means if I for each over an empty array when I expect a full array, it'll still work just fine. Nothing's going to break Similarly if I have this address interface and I create this empty address that matches that interface then If I have no address I can turn this empty address and it's still type safe What should these methods do depends on my use case that that's up to me to decide This is your call as a developer which one is going to make more sense in your code base That's fine. This now helps you with error handling It helps you have to spend less time handling weird edge cases because your type system is going to handle that for you Returning null When you're expecting a real value is usually very rude PHP itself will sometimes return meaningful value or false on error That is flipping your developers the bird don't ever do that Because that's saying you could get back a real object or a Boolean which one you have to check yourself every time and nothing You do is going to be type safe. I can't even give you back an empty array No, don't do that. It undermines your type expectations It forces your users to throw if statements around everywhere. Just don't do that But use return types. They're cool. What else we got? So let's have a look at this code again. There's a similar to the last example we looked at and We've got this distance between Function that takes two addresses and computes the distance between them. Okay but You know that then what is that supposed to be is that an integer? Are we gonna round off to the nearest mile or kilometer? Are we gonna turn it to the nearest, you know, three decimal points? What is that and then it continued shipping? Are we rounding off to the nearest dollar? Maybe I don't know What does distance between return? Probably not gonna be an object So what do we do instead? Oh We just tell it what kind of primitive it's going to be. Oh Oh, oh We're going to assume it's whole miles around a bit. It's an integer We can now type hints on integers and floats and other scalars In other words, if you ever see Andrea folds or Anthony Ferrara at a conference, you owe them a drink Because they're the ones who made this happen PHP 7 now supports scalar typing all optional all opt-in. I still suggest using it We added float int string and bool not note. It's int not integer bool not boolean Now there are no aliases for that But that means the full list of types you can now type in in any parameter to any function or method and Return from any function or method you can type on an int a float string bool array Collable which means thing that you can put parentheses after and PHP will execute code Or any class name internal or external This means you can type pretty much your entire code base like 98% of your code base and Build the logic into your code to control the shape of your data Why is this important? Well, let's look at this address interface. We saw before What does get street return is it a string with a a street number and Street name then maybe an at an apartment number is it an object that has those separate properties which one makes more sense Either one is legit. Both of those are completely valid ways to build a system depending on your use case But I can't tell which it is now I can I Know that gets three disposed to return a string cool Now I can build my logic on that assumption safely and I can type into my constructor for it Safely and if I get passed on like it's not a string it gets handled Course PHP typing has traditionally been very weak and I don't mean that as an insult. It's called weak typing So let's create a new address like a couple of new addresses What's the problem on this slide who can point out the bug here zip code should be a string Yes, because US zip codes are not five digit numbers There are a five character numeric string because in certain parts of the country, especially the Northeast Such as Massachusetts the zip codes begin with a zero what happens when you have a string literal But that begins with a integer literal that begins with a zero in PHP Octol It's rendered in base eight So with this code what happens you pass the value 1099 as the zip code to that address which is probably not a valid zip code not even in Boston and That gets into your database and a month later You read it out and try to compute something off of it and your code breaks because you have this weird 1099 value as your zip code Where did that come from? You came from here because you have a bug because you weren't tracking type But that's what PHP does because an integer can be converted into a string transparently this was the biggest dispute in PHP 7 in terms of scalar typing. It's really what kept Scalar typing out of the language for so long is what do we do with this use case? Do we be extra pedantic or not? The end conclusion was both Because PHP it's actually a pretty elegant approach once you get used to it by default PHP is in weak mode In this case variables cast their students scalars will cast like we're used to You pass an integer to something that wants a string. It knows what to do with it. It'll accept it You pass an integer to a float it knows what to do with it You pass an empty string to something that wants a boolean. It'll figure out you probably meant false and you know the only thing that Does not work naturally like that is if you pass a string to something that expect an integer normally PHP will just truncate that with Scalar types it will truncate that and throw an e-notice. So because that's pretty much always can be a data loss problem So this is well and good for backward compatibility, but does undermine type safety So Stick this line at the top of your file Declare strict types and now it's in strict pedantic mode and now if you pass something to This constructor that is not a string Not actually a string not an integer that looks like a string, but actually a string you will get a type error Which means that your code can spot it your tooling can spot it that you know like your static Analyzer your IDE can spot these errors for you before you even execute your code You can find bugs before you even run your code before you even write tests for it. You can find errors But note this affects calls and returns from this file only This is the quirk and the trick with strict typing in PHP This affects calls in this function in this file only so if we try to return something that's not a string from get zip then That will fail if it's an integer that could get converted to a string It will still fail in strict mode. However, it does not affect the constructor Because the constructor is called from elsewhere. It may or may not have been coerced into strings inside the constructor I have strings guaranteed But whether or not PHP objects To passing an integer here depends on where it's called In our set of file where we're calling from we declare strict mode that will impact Right here that will catch this error because we have an integer. We're calling to a string We're saying PHP on my code be pedantic not on the code in someone else's file on my own code only But it will not affect The you know the get address or gets zip methods actually the returns in those classes This is a little weird it feels odd at first But once you once it clicks it makes a lot of sense and it's really intuitive in Strict mode the only casting that happens the only coercion is an integer will convert to a float without Complaining because that's safe to do in the ninety-nine point nine percent case and most languages do that if you run into a case in where you're in strict mode and You know you still need to convert something you can either convert it yourself and cast the variable yourself or redesign your code I would recommend redesigning your code because if you have a type mismatch There's a pretty good chance that you're doing something wrong and don't understand your problem space Good data modeling and good type checking help you understand your problem space better When should you use strict types? I'm gonna argue almost always almost always The exception is when doing input because let's face it. We're on the web. Everything comes in as a string No matter what it is. It comes into us as a string So my recommendation where you're doing input leave that in weak mode that passes off to some other part of the system That gets cast automatically and then you keep everything else in your system in strict mode. This catches the most bugs That's Reduce the number of tests you need to write this really helps you drive good design It means you have to keep your input output operations separate in separate files from the rest of your code base Which you should be doing anyway, right? Right. This is also a documentation. Who's heard the phrase good code is self-documenting. You don't need Documentation for you don't need comments, right? Anyone who says that and is not using strict typing with scalar types is a hypocrite This is in line documentation that cannot go out of date Because if it goes out of date your code will complain that you PHP will catch this fact for you This is good documentation Of course, there is one BC warning here We do have a couple of new reserved words if you have a class or a function named in string bull or float That will now break that's already been fixed in Drupal 8 and in symphony and most other systems It's real easy to fix on that Well That's heavy That's a lot That's cool Let's try something a bit lighter. Shall we? You've probably written this code at some point in your life You're doing a sort you need to provide a custom sort function and Because of how a sort works to sort things you return negative one if the first one is less and zero if they're equal and one if it's greater than and I Always go to look this up. I can never remember which number. I'm supposed to return for what I really I Can never remember it and it's long and it's ugly and it's stupid and we don't have to write that anymore there's a new Token in PHP new operator in PHP 7 called spaceship This is actually what it's called in the engine that the name of the token and it looks like this Because well, it kind of looks like a spaceship, right specifically a tie interceptor See it arrow and Aaron. Yeah, right. Okay. This is exactly equivalent to that previous slide a Spaceship B which means sort these things and return one zero negative one, whichever it's supposed to be and I don't even have to Remember, which it is a comes before B Or you compare those great Of course, that's not the most useful example most k times when you're doing a custom sort function It's not the degenerate case doing something more interesting like comparing two people I want to sort them by last name and then first name seems reasonable Not a abnormal thing to do and this is what you have to do for that in PHP 5 Return negative one here one and L to get nesting and and which zero is this and yuck That's long. Is there any way we can make this shorter? Well, I guess we could use turnaries No, we can't No, no, no, we can't But we can do this Spaceship between two arrays compares the first items and then the last items if The next items if those match so this will do exactly what that last, you know huge morass of code did Sort these two objects by last name and then by first name You want a third property in here like a middle name or whatever? Okay. Just put a third Value into each array done That's a spaceship operator very targeted very useful in that little narrow place Number four you written code like this at some point, right? Right, okay But this is ugly and boring and redundant. So At some point PHP added I think was in five four probably maybe just five three This collapsed short-turn area does it if the username is a truthy value use that else return anonymous Okay, that's fine Unless the username variable doesn't actually exist which happens in PHP more often than you think especially when you're dealing with the ray keys Do we deal with the ray keys? occasionally Well, then you get this error undefined variable or undefined array key Who's seen that error at some point this week? Yep, so the way to check that is You have to check if it is set and not know Okay, well, that's just again ugly and long Fortunately, we now have in PHP 7 null coalesce Which works like this? It's a double question mark operator Pronounced username equals username what anonymous? I'm pretty sure that's the way it's actually pronounced This is equivalent to that This will return username if username the variable is defined and is not null Else it will turn the next item It's also chainable So you can have layers of default so You can say You know the username is going to be the submitted arrays username. What user username? What anonymous and Then whichever is the first one of these that is defined and not null will get used But be mindful here. I'm saying not null a False defined value will still pass. So I've submitted username here is numeric one or excuse me Is that numeric zero or is an empty string that'll still pass and get you and be what gets used number five Cryptography is hard Really if you think you understand it, you probably don't bite kind of by definition To do modern cryptography. Well, you need to be dealing with really really really big random numbers Any crypto people in the room? I apologize for the oversimplification. I'm going to give just now But you need really really big random numbers And you need to be able to generate lots of really really really big random numbers very quickly Problem computers don't actually do things random. That's not actually a thing So you need something that is sufficiently random in order to do this kind of logic Now at this point you're probably saying Larry that doesn't php already have this kind of solved. I mean we've got the ran function But Rand is not actually random It's extremely easy to predict what the next random value is which means if you're using that for Cryptographic for cryptographic purposes, please stop now There is M crypt that's not really safe either and by the way the library hasn't maintained in oh Dear since php4. We're still a thing if that tells you how long ago it was So, please don't use that either You can read straight from dev you random which will give you operating system provided really random values Problem that only works on Linux and Mac and only if the web server has access to that device Which it may or may not depending on the configuration. So that's not really reliable either There's the open SSL extension, but it's hard to use and not guaranteed to be enabled and Open SSL doesn't exactly have the greatest track record recently on security either does it Si Rip what we really want we really really want is Cryptographically secure pseudo random number generator or more properly pronounced C-Spring Some people will tell you it's C-Spring, but I don't see an eye there C-Spring. So do we have a C-Spring in PHP? Well, we do in PHP 7 You want a series of cryptographically secure random bytes? Okay called a random bytes function You get back 16 random bytes not characters 16 random bytes of garbage. Don't print it to the screen It may not do what you expect You want a random integer? Okay, yeah, let's get a random integer between one and one hundred inclusive done and it works and it's easy and it's secure and it's safe and Huzzah if for some reason your underlying engine and operating system have no way to give you Valid safe random values. It will throw an error Excuse me throw an exception Why because if it didn't and it just returned false as so many functions in PHP do Well, then you'd get false which casts to zero when you try to use as a number or to Empty string when you try to use it as string and then you're using Zero or empty string as your random key value for everything and oh boy is that not secure? So yeah, if it can't actually be done securely safely bail out because you don't want to continue bad idea If you're on PHP 7 use these if you're on PHP 5 There is a backport library available that I don't did we get that to 8.1 ng. I don't recall There is an issue to swap out Drupal's random number generator with this backward compatibility library that's been built by the same people who did the implementation for PHP 7 So it's reasonably trustworthy But just like your password hash you can use it and it just takes care of that problem for you And then when you upgrade to PHP 7 it just uses the native versions So side note minor rant here if you're doing anything with security and you're not using This set of functions for password handling and for a random value generation save with me now Please stop you're doing it wrong if you're doing anything in modern PHP. This is the correct way to do Secure random number generation and secure part password hashing do not under any circumstances try and write your own encryption system For the love of God don't try to write your own encryption system And of course BC warning here You now have two new functions if you already had a function in your code base named that well, it just broke But if it wasn't already doing the same thing anyway, I don't know why you called it that so no big deal number six You know what else is hard? testing specifically writing mocks This is writing mocks with PHP unit This class under test down here is What I'm actually want to be testing in order to get to the point where I can test that I need to create a mock builder Disable constructor then get a mock. What's the difference between the mock builder and the mock? I'm not actually sure Then I I can create a straight value object on its own that's fine but I also need a mock builder all the same boilerplate for this other class and Then I've got this domain specific language for slowly writing out how this code should behave in this metal language for defining how code should run and Then I need to use this will return value map and the structure of this array does not make the slightest bit of sense to Me at all. I do not comprehend this. I copy this out of the PHP unit documentation. I still don't quite get it and Finally I can assert true on something yuck What we really want is There's a custom class that we can use for testing purposes only that I can just write code for and not have to write this code that defines code nonsense and Just use that for my test But I don't want to have to have another class and hit the auto loader and it being another file I just want it right there just like the mock is What we want are anonymous fakes? PHP 7 gives you the ability to define anonymous classes For which I have no use cases other than testing but for testing. They're kind of nice These are classes that are used once in this case We're going to create a new class That extends the thing we're mocking Override the constructor to disable it which is this is much easier to read and then that's a load method. Okay Forget this map stuff. I can just do you know, whatever I can write actual code to return what I want to actually return in these cases and I can even return New anonymous classes if for some reason that I need to do that. I can nest them This is much easier to follow much easier to read much easier to maintain then this weird You know mocking metal layer stuff. There are still use cases for that But for a lot of cases Just switching to anonymous class is going to be a lot less effort Anonymous classes can do pretty much anything a class can do except Be serialized anonymous classes cannot be serialized anything else. Yeah, they can do it You could have a new class that implements our logger interface just pass it in directly the service requires the logger Okay, we'll give it a debugging logger right there and it accepts traits to Just like a class any normal named class does We can have a constructor and you can call the constructor pass that a logger interface object to it It takes it and does whatever it's going to do As with anonymous functions, you can write some Extremely impossible to read code this way. Please don't but when used judiciously they can make your code a lot cleaner A lot simpler a lot easier to read Especially in tests if you have a use case for them outside of testing, please let me know I'd actually love to find a use case for them, but at the moment testing is the main the main use case What else is on the list of things that suck? Fatal errors those suck don't they? P&L PHP has two kinds of fatal errors. It has actually fatal and it has things that are called recoverable errors Except you can't actually recover from them This is helpful problem with recoverable errors is that There's really very little cleanup you can do you get a single unified callback that gets called with no context That Doesn't let you actually recover you can't you know figure out your system unless you're doing everything globally which you're not right right It also means that if you have a finally block and a try catch finally that doesn't get called If you have a destructor on an object, that's not going to get called properly. You can't do any real cleanup Suppose you have code like this to this do stuff function It takes a request object and we pass it a user object or a user is not a request I'm sure there's a an insult in there somewhere, but And so this should die It gives you a catchable fatal error Catchable fatal error, but I can't catch it So why is it called a catchable fatal error and if it's fatal why do I have a chance to catch this does not make any sense at all Which is why in PHP 7 there's something you can catch called a type error Which gets thrown stick that in a catch block and do whatever cleanup makes sense in your use case Include, you know, it's some PHP code that is buggy and has a parse error in it instead of PHP just bombing on you You can catch a parse error Why is this useful? Let's say you're including a module file Do you want your entire site to bomb or do you want to just you know be able to clean up and show the user a helpful error message Instead of oh crap the entire site just exploded because someone forgot a semicolon in a module that I downloaded off github Take your pick which one you want there call a function that doesn't exist. You get an error thrown PHP 7 includes a new hierarchy of throwable things Exception now influence an interface called throwable that is built into the engine That's it basically inherits everything that exception used to have on it now moved to throwable Exceptions work as they always have but now they have a sibling called error and We saw some some of these There's a certain error parse error type error arithmetic error a couple of others things that used to be Recoverable errors or parse errors are now thrown objects These are used for Cases where the code is wrong You can catch them just like you would an exception But they're for cases where the code is wrong not the user not the environment the code is wrong Exceptions you you use when the user screwed up errors you use when the code are screwed up That's the general guideline You probably will not catch Errors locally the real use case for them is something like Drupal having a global error Handler rather than exception handler so that if you know you have a type error or a module is buggy or whatever Drupal can catch that or whatever your framework is can catch that and say oh there is this problem I will log it like a Good piece of code so that we can debug it later I'll show a nice message to the user saying that such-and-such broke and will report to the administrator and it's okay Rather than just saying fetal error or whatever no bad These are most of these cases you couldn't continue any way with your code. It's a matter of You know cleaning up nicely After yourself Again back compatibility warning here It's a couple of new class names that are reserved if you had an exception named those already or a class name those already Those will now break Your odds of that are again pretty low There's also the global exception handler in PHP 5 you probably did this a lot you said an exception handler Globally so that this is basically like dropping a tri-catcher onto your entire script and then whatever your function is gets called Yeah, this is your you know your catch block essentially and you can type into it on an exception because it'll catch anything Problem in PHP 7 it could also get an error at that point. It's caught by the same function So you have to type into on throwable, which is the parent of both error and exception Of course that breaks on PHP 5 because there is no such thing as throwable So, you know just don't type into it. This is what we did for Drupal 8 Sorry, whatever. Oh, let's see. What else sucks that we can fix assertions a shirt assertions kind of suck Assertions work like this. Who's actually used assertions in PHP? Wow, that's more than I expected Very few people use assertions in PHP because they don't work right the basic idea is you have this function assert and That asserts that this statement is true and if it's not true you die with this additional error message this is for Most useful case is like extended type checking for things that you can't do It's just the normal type system itself like in this case This definition array has to have these keys to find on it if it doesn't the coder screwed up So we'll just die right here problem assert as a function and You can turn it off in production. That's the idea so that this happens only in development on production you turn it off turn assertions off and then you don't get those error messages there because Presumably you fixed them all in code by them But it sort of a function which means that the body of the assert that those is set checks always run Which is slow, but then you're not doing anything with them So the alternative that you're expected to do is pass in a string that will get eval It was right Yeah Like I said assertions kind of suck So in PHP 7 we have this thing called expectations Why are they called expectations? I have no idea what they really mean is assertions don't suck So now we can turn on assertions these two any sets and Then we can have our own errors that extend from assert error, which is one of those errors We just talked about and now assert is no longer a function. It's a language construct, which means When it's enabled this will do what we'd expect if this is true good If it's not throw this error which we can catch the report and say oh by the way developer you screw it up go fix it in Production we turn assertions off this entire line disappears from the code base So it does not matter how expensive that check is It doesn't happen in production Basically assertions actually work now Drupal 8 actually does use assertions in a few places. We added support for Assertions which mainly means we have a shim that makes them look more like the PHP 7 version you still need to put them in quotes so that they They get eval and there's a couple of nice utilities In the system for asserting Let a rake certain are keys exist or that something is an associative array or something is a numeric array and stuff like that Number nine who's worked with iterators in PHP? Far too few hands are up right now It a razor fun, but the old way of doing iterators was hard Let's say you want to You know take two different data sets and run through them as if they're a single data set. Okay. We let's use iterators. Oh dear We have this class. We're going to pull data from a remote data source So we create a new whole new class that implements iterator Keep track of three properties implement one two three four five six methods Some of which don't make any conceptual sense And yes, this is not the right way you want to do this as a single call then cash it But I wanted it to fit on a slide And so we've got all this bookkeeping we have to do ourselves in order to Iterate over this data set nicely You can also get a an iterable object back from our database layer because we use PDO and PDO results are iterables and then we can create this append iterator and Pass both of those to it with this append call and now we can for each over them all as one big thing and that's yuck Nice capability way too much code Since PHP 5.5, which means every supported version of PHP that exists you can now use generators generators are Worked like this When a function has a yield instead of a return statement when execution hits that yield That gets returned as if you were calling next on an iterator The next time you try to iterate it it picks up where it left off and continues So when you stick this function or the result of calling this function into a for each loop it will Run through return yield that value to the loop next time through pick up where it left off keep going hit the next yield and so on I Could give an entire talk just on Generators, I'm not going to there are people who do But this let's us take those two slides worth of code and reduce it to just these two functions That's in PHP 5.5. If you're not already doing that, please start What's new in PHP 7 is? We can now yield from another iterator so this Get values function does the equivalent of the append iterator, but it's a lot easier to read. It's a lot less code And it does exactly what we'd expect yield all the stuff out of that first iterator Then you'll all the stuff out of the next iterator You can also now return from a generator. So when the iterator is done, there's still a value at the end Your odds of using that yourself are pretty small. I'll be honest Both of these capabilities were added primarily to support co-routines What are co-routines again? That's a whole talk on its own Short version co-routines are a way to use this kind of stuff to write no JS in PHP If that sounds cool, like you're correction. No JS in PHP only better If that sounds cool, look up something called icicle.io. It's a PHP library that's using this kind of technique and This this kind of capability will makes that better And finally number 10 Quick pop quiz. What does this code do? Variable variable so what happens here? I don't know is this the bar key of the foo variable which is then De-referenced to get the name of a string which is an array that we then get Baz off of How about that one Oh now you're just messing with me I don't know I'd have to actually look it up and see what actually what happens for these actually I haven't written down on my notes because I'd never have figured it out otherwise in PHP 7 that has been cleaned up the variable syntax is now uniform by which I mean it's always read left to right This was not actually done for usability. This was done to make the engine Smoother and cleaner, but as a nice side effect. It makes it a lot easier to read the code, too Everything now reads left to right So in this first case what happens? We have foo We treat that as a callable and call that the result from that is an array. We do reference bar off of that And that's itself a callable and we then call that I Would never actually write code like this But if I run against someone who does I can just look at it and pretty easily tell what's happening And what's actually going to run in what order? The second one I've got an array literal Which I can then read the first there is zero with item off of And access a property read off of that Again probably shouldn't write code like this, but if someone does it's very obvious what's going on All of these examples. It's pretty self-evident. You can just look at them and see read left to right to see what happens Which enables some really interesting things like you can wrap any expression in parentheses and Then dereference it as an array or as an object or call it literally Which means You can do something like this and have a function you define literally and then call immediately just like in JavaScript Okay, maybe that's not the best example But you can and it works more importantly you can now If you have a a callable on that is a property of an object Throw parentheses around it and you can call it directly without assigning to a temporary variable Everything just becomes more logical and more and easier to predict both for you and for the engine Of course one BC warning here This means that all those complex cases. We just looked at a moment ago what they actually mean Changes depending on which PHP version you're in solutions that don't or You know wrap stuff in parentheses or sometimes curly braces And it will force an order of operation. You can still do that in PHP 7 if you really want something to Evaluate differently, but at that point what are you doing? So that's it. That's the top 10 features in PHP 7 as far as I'm concerned, but This is PHP 7 and we're not done yet PHP 7 is fast Really really fast How fast is fast? Faster than a cheetah trying to imitate a kangaroo Which is a line I use just because of that picture Why how did PHP 7 get so much faster? Well, the reasons are pretty nerdy. I'm not going to be going to them in detail long story short the Engine the PHP engine itself is written in C and the people writing that engine Rerote most of it to be way faster in ways that make sense for writing fast C code that has nothing to do with the PHP code API's for PHP code barely changed at all But the API's for writing C extensions for PHP changed dramatically because they're now crazy fast So I'm not gonna go into all the details just suffice to say well Let's ask Rasmus founder of PHP. He ran some benchmarks last fall This is Drupal 8 as of I think October. So this is a couple months old now In terms of requests per second, so higher is better I Like this HHVM still an improvement, but not as much Okay That's pretty good Wordpress very similar, you know toodling along on PHP 5 In this case faster on HHVM than on PHP 7 Also, we just saw 2,500 requests a second for Drupal 8. Here's wordpress 4.1 at 6.04 Oh, yeah, it's be be be be be very similar story. Okay. Okay. Okay. So in this case Much better than HHVM. I was HP 7 Media wiki same same pattern in this case again has a clear edge, but still both are faster Basically every time I see Zen put out a benchmark it proves that's HP 7 is faster than HHVM Every time Facebook puts out a benchmark it proves that HHVM is faster than HP 7 But what both agree on is they're both way faster than 5.5 or 5.6 by 50% faster If you have a CPU bound application where most your works on PHP You can see you can see 50% improvements Just by switching to PHP 7 there are companies that have done have done switch to PHP 7 with very very large codebases and and the memory savings and the CPU savings they've been able to turn off half half of their server cluster Just just by switching to PHP 7 if you take nothing else away from us talk PHP 7 is the number one performance improvement you can make on your site today But some of you are probably scared because because it's 7.0 and I don't trust 0.0 releases of anything and and Historically, that's a fair statement to make let's be honest But not this time Drupal 8 Zen, Zander, Valera, Vee, Yee, Jent, Magento, Lim, Slaver, Press, Wordpress, K, Doctrine, HVM, HVM, Eidman I don't know what to call those code pine logos for already fully tested and and a full test to be passed under PHP 7 This is what this is what it has made us what it does for you PHP 7 is the best test of PHP in history because because all of these are on the next object They are very code based. All of their weird edge cases is on 7, 7 or 6 or 6 months In fact, in fact, PHP 7 was out in November, November, it came out, it came out in December Why? Why? Because it was in a minute, a minute, Drupal, Drupal, with our test, our test has been found in funds in the memory handling of PHP 7.7 Because we are the university of test for a very, very high-end PHP And not actually made it out of that, it was in the array handling And so they found that, they fixed that, they found it and sat on the test for weeks to make sure it didn't break and it didn't melt So PHP 7 is the most tested, most battle-hard 0 release of PHP, PHP ever Drupal 8, the Drupal CI structure running on Drupal.org can test everything with PHP 7 Drupal 8.4 does pass its entire test rate under PHP 7 that has for months As does, or as do almost all Drupal 8 modules If they don't, well, go fix that module What about the hosts? Do we have to wait for the hosts to catch up? Funnish to mention it, PHP version.info It's a community run site except for call requests for tracking What hosts are offering? What version of PHP? As of this past weekend, when I last checked, there were 34 hosts listed there that were offering PHP 7 now And that number's only count is going up The last time I looked at it before this talk was 20 some-odds So that number is growing, you can pick any of these And commercial, yes, platform.sh is a PHP 7 ready host It's one line in config file So if you want to run Drupal 8 under PHP 7, you can do that today That is PHP 7 in a nutshell It's ready, it's awesome, it's fast Use it now, it is the correct way to run Drupal 8 Thank you We have a couple of minutes left, so we've got a microphone right here for questions Coming up? See someone coming? Is that... There's a switch at the side, I think Or just shout it out, I'll repeat it Give it a second Just shout it Drupal 7 and PHP 7 We're at what, like 98% test passage There's a few weird things in the session handling It might run anyway, I haven't tried it But that's getting very close That is something people are working on and we want that to work And it will soon If you've got a site now, you're using continuous integration for your testing Which I hope you are Travis, CircleCI, whatever your CI system is Add one or two lines to a config file and you can test your own site under PHP 7 as well And make sure everything works there So very, very close, we'll get fixed soon If you declare strict types in a file, are you required to actually declare types? No, you're not What that affects is if you make a function call from that file And the function you're calling has a type specified PHP should be extra picky about that call If you're returning a value and you have specified a return type Then PHP should be picky about that return But you can put your code into strict mode Specify no type to yourself And it will only affect the calls you make to other code that is typed So everything is completely opt in, I just recommend opting in everywhere Global configuration for strict types No, there is not, because that means code will behave differently depending on any setting And PHP has learned the hard way that is an absolutely atrocious idea That you hear, you can have a third-party library you pull in That is using strict types internally I don't want to use strict types for whatever reason So my code I leave in loose mode That code can still be in strict mode and still do all of its strict checking at once And I can ignore that fact in my own code Because if it's in strict mode in that file, I'm not, I call that function That function call is still weak mode So I am unaffected by it So that's why it can't be a global setting The question is, is PHP going to adopt function overloading Now that we have the strict types From what I see on the PHP internals mailing list I highly doubt straight function overloading is going to happen anytime soon There is discussion of union types, which may or may not happen That's still up in the air There's discussion of adding the ability to opt in Something to being nullable As either a return or a parameter that is going to vote soon, I think But yeah, function overloading I don't see happening in the near future And the zeitgeist of what people are saying and how the engine works The question is about a JIT, just in time compiler So Zend actually was starting to work on a JIT for PHP And realized they couldn't By the time you made it work, it didn't give you any benefit And they solved that problem by rewriting the engine and calling that PHP 7 There is talk of adding a JIT at some point Whether or not it's even valuable anymore with the speed improvements in PHP 7 Is debatable I don't know, it may or may not be useful Frankly, at this point, there are other optimizations to make that are more valuable I would say, so maybe But it's a different architecture, different runtime setup So not a priority Okay, so commenter saying he has a Drupal 7 site on PHP 7 And the only error is somewhere in the GD image handling And beyond that, everything is working fine So as I said, it's not perfect, but Drupal 7 and PHP 7 are really, really close I've never tried doing an Ubuntu 12 to 16 update Or a 1204 to 16.04 update My update, I just recently upgraded my own system from 15.10 to 16.04 And it went very smoothly I do know a lot of the package names changed between Ubuntu versions So you may be caught by that, I'd say Google around for it Or these days, if it's a VM, just build a new VM They're cheap If it's a production server, move to a container-based system And then that becomes trivially easy I happen to work for a container-based company, so I get to say that There's 16.04, okay So yeah, Drupal 7 on Ubuntu 16.04 Yeah, Ubuntu 16.04, which is just released They're LTS, ships with PHP 7 out of the box So if you want a nice, stable PHP 7 distro that's LTS stable, the new Ubuntu is Anyone else? Someone who knows nothing about PHP is seven a good place to start Yes Most of the system is the same We added, I shouldn't say we, I didn't work on it directly But the internals team added a lot of new capabilities, a lot of new features But the basic language is still the same So aside from the stuff I just talked about pretty much Everything else you do in PHP is going to be the same between five and seven That said, PHP 7 hosts are not impossible to come by There's a couple dozen of them already The language is much better And it will also have security support for longer So I go ahead and start there And I think I'm getting told I need to stop talking So thank you everyone for coming, enjoy the rest of the conference