8 Things You'll ❤️ About PHP 8 | PHP 8 New features [2021]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to gary clark tech with myself carrie clark and in this recording i'm going to look at eight new features in php eight which i think you're gonna love before we get into that some information first i record high resolution so you don't have to watch on a blurry screen choose high definition if that works for you would you like to join a growing group of php developers and take your skills to a new level if that sounds like you all you need to do is subscribe and click the little notification icon and welcome quick check of my php version as you can see i'm using php8 if you're using a mac and you've not yet upgraded check out my video on how to do that also i'm using phpstorm so i'm going to set my php language level to eight so the php storm doesn't think i have a lot of syntax errors all of my code so let's get into this first off we'll have a quick win a quick easy win so we can now allow double colon class on objects the same way we can do under classes i'll demonstrate this to you now so i've got a class which i've just called demo class and i'm gonna access class using double colon class and so doing this i should be able to print out a string version of the namespace and the class name the next thing i'll show you is match expression so whereas you currently have switch blocks which are quite clunky and if you want to set something to a variable you have to do it for each case match expression is much more streamlined and it's also more strict so we'll print out the result of this switch block the result is bar because that matches the value of one what about if i change the one to the string one what do you think will happen do you know the answer we actually get the same thing because it's obviously using a double equals comparison rather than a stricter triple equals so that can be a bit of a gotcha i don't think it's ever got me because i don't often use switchblocks to be honest i think they're quite overused now let me show you how to replace this with something more slick this is a match expression and i think you're gonna like it so i pass in the number the same as i did with the switch block and then inside of these curly braces i create what is called an arm for each option and as you can see i'm using the double arrow operator very similar to assigning values in an array but what i hope you've noticed is that i'm actually assigning the outcome or the output to a results variable which i only have to do once we'll try this with an integer one first as you can see we get power back let's change this to a string as you can see i'm getting a red squiggle so this should tell you that this ain't going to work fatal error uncut unhandled match error on handle match value of type string not only do the values have to match but the types have to match also because we've got triple equals strict comparison here's something else i really like about this i can put more than one match value on one arm so for foo i'm gonna have a choice of zero and one and four bar i'll leave that as two just to be clear if the number is zero or one we'll get food back and if the number is two we'll get bar back let's test that out so we have food back because the number zero change that to one run it again foo change it to two run it again bar a lot of possibilities there very powerful i think next up is constructor property promotion up to now in php when you create a class you have to declare uh the properties and then you have to pass them in through the constructor and then you assign them we can clear out all of that stuff and do it all in one line and this is how you do it by simply adding a visibility keyword in front of each of these parameters i turn it into a declaration an assignment and a constructor parameter all in one line how cool is that so there are some circumstances in which you won't work for example you won't work on abstract classes so feel free to read the documentation or you can do what a lot of us do and just figure out what you can and can't get away with as you work along i'll print something out here i'm going to access one of the properties the property y just to prove that it works and there you go i'll move these all onto their own lines which is probably how most people are going to do this and i'll just show you a couple of things to watch out for so say for example i wanted to have a default of null as you can see angry reds wiggle because don't forget this is now a declaration so in order for this to be nullable we have to make sure that it can be a nullable or float type so we'll try this out as you can see cannot use null as a default value for parameter y of type float so it needs to be of type null or float and the way that we do that is we just go and prepend a question mark in front of our float type declaration and hopefully this should work there you go back to working again property promotion isn't going to work in all circumstances but check the docs or just figure it out as you go along my favorite new addition to the language is named arguments it's been a long time coming and it's something which is quite common in a lot of other languages let's have a look at how things work at the moment so this is php's array fill function it takes three parameters start index count and value they all have to be passed in in that order and for that reason we call them positional arguments but now we can actually use their name and pass them in in whatever order we like if we're using their name and the syntax looks like this it's the brand name minus the dollar sign followed by a colon followed by a value let's run that okay and now what i'm going to do is i'm going to switch around the order of these so i'll put the start index at the end and we'll go and run it again and it works exactly the same now you're probably thinking though we didn't actually gain much from that we might as well have just passed everything in using positional arguments but named arguments really coming to their own when the functions have defaults you probably know this function html special chars it takes four arguments a string and then it takes three arguments which have defaults as you can see the last argument double encode has a default of true but if i was happy with the rest of the defaults the flags the encoding but wanted to change double encoding to false i would have to pass in values for those two other arguments even if i was leaving them the same as the defaults which sucks named arguments to the rescue so let's have a look at how we've had to do it up until now i'll stick with the defaults which i've copied and if i wanted to change any default values which came after that such as a double end code here i'd have to include those things but now i can get rid of those and all i have to do is just specify the name double encode and then let's go to the terminal and test this out okay great nothing broke everything's working there what you cannot do is this once you've used a named argument you can't suddenly start trying to put positional arguments in there that will not work cannot use positional argument after named argument and just for clarification anything which isn't a named argument will be treated as a positional argument up until the point where you introduce a named argument i'll make string a named argument just to get this working again okay all good there the possibilities are endless really we named arguments gives us a lot more power and flexibility than what we had with just positional arguments here's a cool little trick which might interest you so say for example you've got something like html special charge but you know that you're always going to want to set double encode to false what you can do is create a little reducer function which wraps around the html special charge function and i'll show you exactly what i mean so i'll go and grab this stuff here which i commented out i'll paste that in there and so i've called this html special chars on encoded and what this will do is return html special chars but with double encode always set to false instead of the default which is true and therefore i can do the same thing but i only need one argument for my function that argument obviously being a string which isn't a default let's test this out so i'm going to set the return of html special chars unencoded to an unencoded variable and i'm just going to pass in a random string over to the terminal and there you go nothing breaks still working so that's named argument so one thing i'll say about named arguments is there is probably more to this than any other things i've shown you so do check the docs next up is an old safe operator so in this example here i'm trying to get a country property off an address which i try to get off a user which i try to get off a session any of those three things could be null so instead of having to go through this whole charade with all these if statements with a null safe operator we can actually do this after each access attempt i'm placing the question mark which is checking for null and if any of those things are null then country will evaluate to null and no exceptions are thrown it just works seamlessly as you can see at the top there i've set session to null so it means when it gets to this bit it's going to short circuit and country will become null so user get address it won't even get that file already be evaluated to know before then so i'll remove the old stuff and let's try this out so i'm just going to file dump country and let's go over to the terminal run this as you can see it's null now ideally i'd like to try out one of the things for the middle user or address so how can i do this what i'll do is i'll create an anonymous session class and i'll have a user property on that which i'll set to null i think that should do the trick for us so public user equals null so what i'm expecting to happen here is it'll get past session because that has a value however user is null and it should short circuit there which it does and the whole expression evaluates to null so that's an unsafe operator and i like it next up we have union types so at the moment if you want to type in anything we're only really able to specify one type plus null if you want to be able to say that something can be int or float we have to do it in the dot blocks like we've done here but that doesn't actually offer you any protection that's more like just for information and i'll demonstrate exactly what i mean by setting this number to a string so the dot block says it should be an integer or a float what i'm actually going to do is just set it to the string foo no exceptions will be thrown it will just set it to whatever i want it to be set to so set number through and then we'll just print out number get number and then we'll go over to the terminal and have a look and there you go so our dot block really is just that it's just documentation it doesn't actually enforce anything but with union types we can now do this i'm going to get rid of all my dot blocks and i'm going to grab the types and i'm going to use them as type hints this is a total double win because not only is php going to check that we have the right types we also have self documenting code if you start to feel emotional right now i totally understand so i'm going to leave the set number to a string and we'll go over to the terminal run that and we get an error argument number one number must be of type int or float so let's change it to an integer and we'll go back and check that works this time let's change it to a float perfect okay now what if you want this to be optionally null well you can't actually do this you can't place a question mark in front neither can you wrap it in brackets and place a question mark in front there may be a proposal for that but it won't work at the moment what you have to do is be explicit and just add null as another type the same way as we've done for int and flopped so number i'll need to change this and also make this nullable so that means i should now be able to set this to null i'll change my print to vardham so that we can see it over to the terminal let's go and have a look no there you go union types is excellent i think next one is string contains i think this one's been a long time coming if you wanted to see if a string contained another string up till now you've had to use string pause or combination of other things now you can do this which semantically is a huge improvement so i'm just checking if needle is in the obligatory haystack and then we'll go over to the terminal and it should say found or not found that's not found so i need to come up with a string which is in haystack so stack back over to the terminal found okay excellent we also have string starts with and string ends with so can you guess what these do again semantically a lot better first i'll check if haystack starts with stack and if it does or if it doesn't i'll print out a message does not start with stack so i'll change this to hey back over to the terminal does start with i'm pretty sure you figure this out by now but we'll do string ends with just to be thorough so does haystack end with hey i'll just tweak these feedback messages does not end with let's change it to stack back to the terminal does end with so there's eight things which i hope you love about php i think that the language continues to go from strength to strength which means that your code will go from strength to strength too i'm hoping we can get some discussion about topic going on down in the comments below if you want to get involved tell me which new features you like have you used php8 so far if so how have you found it is there anything you can share that will help other php users like yourself it would be great to hear from you and i always look forward to reading comments if you've enjoyed this one be sure to give it a like and don't hesitate to share if you want to help others and if you want youtube to show you more of my content all you need to do is subscribe and click the little notification icon i release new material at least two times a week and details of my schedule can be found on the discussion tab of my youtube channel home page
Info
Channel: Gary Clarke
Views: 3,254
Rating: undefined out of 5
Keywords: Gary Clarke, learn PHP 8, php 8 new features, 8 things you'll love about php 8, upgrade to php 8, learn php for beginners, learn php web development, php tutorial for beginners full, php tutorial, php course, named arguments php, named arguments, keyword arguments php, promoted properties php, union types, union types php, php string functions, php match expression, php switch block, php nullsafe operator, php check if null, php 2021, php 2021 tutorial, php update
Id: ZZxCj91NjWg
Channel Id: undefined
Length: 14min 26sec (866 seconds)
Published: Thu Feb 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.