What's new in PHP8.1??

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
recently php 8.1 has been released with bunch of new features and editions in this video we are going to cover the most exciting features like anim's read-only properties first class callable syntax intersection types and much more [Music] hello everyone i am the co-colleague and welcome to my channel on this channel i do coding tutorials and challenges as well as some tips tricks and advices if that sounds interesting to you consider subscribing in this video i'm going to cover the most exciting and interesting features if you want to see the whole list of changes and features you can check the video description for the links every line of code which you will see in this video will be available on my github repository and i'm going to put the link in the description as well all right first of all let's have a look at my project setup i have created composer json index.php and source folder inside sourcefolder i created each file for each feature i'm going to explain in the index.php i enabled to display errors and included the first file which is eminem's php in the composer.json i have some basic things without loading which i don't even need and in the require i have php version to be above 8.1 if you're gonna create a production ready application or if you want to migrate your application from older version to php 8.1 you definitely need to specify php version above or equal to 8.1 in your computer json alright let's start the very first feature which is anoms first of all let's understand what is ana anoms define custom type that is limited to one or discrete number of possible values atoms do exist in many programming languages with a variety of different kind of features in php nrms are special kind of object they are much like classes and they have cases which are single instance objects of that class this means that nm cases are valid objects and may be used everywhere where valid objects can be used alright let's have a look at the basic anim to define an enum you need to use the keyword enum then just like a class you give it a name the coding standard is to give it a pascal case name and right here we define cases case active case pending case deleted and we have simple user status ana and right here i'm accessing the active and let's actually dump what is the active value i'm going to save this and have a look in the browser i actually have two different php versions running on the left side i have php 8.0 and on the right side i have php 8.1 if we reload the code on the left side we're going to see syntax error however on the right side this works fine and we get output num user status acting why do we get this syntax error on the left side simply because php 8.0 does not support enums okay let's go right here and talk more about it so the anim as i mentioned is much like a class and cases are much like in single instance objects of those cloud of this class so right here active we can think about active to be an instance of user status instance with the active uh case okay so again have a look on the right side right here so if we just try to get the name of this active just like this we can print the name all right let's actually look what's the output when instead of war dump we use print r okay and i'm going to print the whole active okay reload the page and right here i see an m right here which has name acti okay so those are basic anoms however more realistic is that you're going to work with baked animals what are baked atoms each case right here can have its corresponding value for example active can be 1 pending can be 2 or deleted can be 3. these values can either be integers or they can be string but we need to define right here a type what type of value each case is have so in this case we're going to type int just like this we have now baked anna and what happens when we just print active let's have a look so right here we get also value corresponding value so just like we access right here name we can access in the same fashion value and in this case we're going to see one as an integer right here so those values are basically used to save the corresponding information in the database and later we can create an instance of this nm from that actual value all right let's now consider the following case that we have an id not id but let's call it status which we get from the database we don't know what its value but actually it is one okay so we get that status from the database and i want to actually create a num from that status one so i can use on a user status nm method static method called from and i'm gonna pass status right here and i'm going to assign it to a variable in a status and finally i'm going to dub dump this anime status so right now we're going to get in user status active of course if i change this value into 3 we're going to get right here deleted however if i change this value to an integer number which is not a value of any of the cases right here we're going to get error fatal error okay there exists second method which is called trifrom the triphone basically tries to create anom from the given value but if it doesn't exist then it doesn't throw an error instead nm status will become null so in this case we can have this now basically if you just don't want it to be now right here we can assign uh the default value to be user status pending for example save that and reload and here we see pending enums can also have methods for example i'm going to create a method right here called text which will return the corresponding text value based on the case of the anim so let's actually delete everything right here and i'm going to paste the method right here so we have public function text and if this we use the match php 8.0 syntax feature match if this is active the user is active for pending user is pending or deleted user is deleted and down below i'm going to simply print user status active text remember the case is much like an instance of this atom so we can call this text method on that instance save that and have a look in the browser we see user is active if i just try to call the text method on pending we're going to get user is pending enums can have also static methods so right here i'm going to create a simple method static method which basically simply returns hello from static save that and down below i'm going to print user status static method save and reload and we see hello from static items can even implement interfaces so right here i'm going to define interface my interface which we can give it also a method hello for example and let's implement our nm from my interface and we have to implement that stub method hello as well down below and let's simply return hello from interface well this is not actually coming from the interface but you get the idea so that now is an instance method and we can call this on a case like we can type hello right here save reload we see hello from interface if i just change this into static of course i need to declare this to be static in interface as well save this and down below i can call this hello save and reload and i see hello from interface in both places all right anoms can actually have constants as well so right here i'm going to declare constant let's make it public constant let's give it a name archived which corresponds to self deleted okay these constants can be a normal constants which have corresponding primitive values like string or boolean or each integer or they can reference to case of the enum itself actually atoms can also use traits right here so if you have a trait which is called mitrate and you want to use that in the enum you can do just like you're using in the class so as i mentioned at the beginning enums are much like classes basically you can use almost everything you're using in your classes you can use also auto loading in name spaces for your anims so my trait and that's absolutely valid so if i add a method inside my trait i can call this method right here on this case all right sometimes we might need to list all possible cases of the items for example we want to display those cases in a drop down and then the user needs to choose what's the status of another user okay so right here i'm going to call user status cases and that prints an array of anoms i can actually put this in the var dump statement save and have a look in the browser and down below we see an array of anoms all right i'm going to leave this code untouched and moving on next feature which is called array unpacking with string values so right here i have that second file i'm going to open that and in the index.php i'm going to include the second one so right here we have numbers and new numbers and since php 7.4 i think we have possibility to unpack array when the array basically is a list when it contains when it doesn't when it's not associative array okay however in php 8.1 we have possibility to unpack the arrays with string values so i'm going to actually dump right here dump new numbers okay and then have a look in the browser and we see those numbers okay now i'm going to create default user which has basically named zura or let's give it name join okay and that's the default user and later i want to create a user and i have some information about the user but i want to merge this into a default user so the default user needs to be merged with the user and the first thing which comes to mind is to use array merge however since php 8.1 we can use unpacking of default user so right here i'm going to type three dots default user and then i'm going to give it a name to be zura okay now finally let's dump the user and have a look we have namesura so this default user can have some other values like surname to be do save and reload and we see zura do and so on you get the idea you can basically use any number of areas right here so i can pass an array directly and unpack it immediately which obviously won't do anything but i can give right here age corresponds to 30 save and reload and right here i see age 30. all right moving on next feature which is called read only properties all right what are read-only properties read-only properties are properties that whenever you assign a value you cannot change that so right here i have class order and user in amount and in the constructor i want to basically accept the information about the user and amount total amount of this order and i'm going to save this up right here okay so that's pretty straightforward and i can i'm not going to do it like this it's pretty straightforward instead i'm going to actually use promoted properties like public string user and public that actually should be float amount so i'm going to delete those two and now i have those promoted properties and down below i'm going to dump new order give it information about the user which is john and amount 100. save that and reload the page and right here i see user join in amount 100. however i can let me save this new order in a variable order is new order i print that order and down below i can actually change the value like the user for example of this order and call it zura let's absolutely valid and down below i can print that order and now i have oops uh would they do wrong semicolon is missing right here below the page and right here i see user is now zero i can define any property i want to be read only so that i cannot change its value later so public read only string user right now my php storm got crazy it doesn't understand what's read only and shows me a lot of red underlines however this will work my php storm simply at this stage does not support php 8.1 so i'm going to save this and have a look in the browser okay cannot modify read-only property that's the exact thing let's have a look on line 19. that's the exact thing i was talking so we cannot reassign value to read-only property okay that's the that's the thing actually if you want to have the latest version of phpstorm you can download it from here so that's early access i'm going to put the link in the video description that basically supports everything around php 8.1 it's not yet stable but it actually works fine i actually already have it installed locally as an early access and let's have a look so here is my early access php store which fully supports php 8.1 and now you see that it basically doesn't complain about read-only even more when i start writing or here it actually auto-completes that for me and down below it also gives me ready to underline that cannot write to read-only property outside of declaration scope so that actually um i'm going to show you one another either another example sorry for that so public string user so you can actually declare user right here but you cannot give initial value right here so if you just make it read only and i'm going to remove from here you cannot give initial value here so the purpose is that it can only be used when initializing okay you can declare this as read-only get right here string user assign it into this user equals user and that's gonna work you cannot reassign it later but you cannot give it to the default value all right let's move on on the next new feature which is called intersection types all right right here i have simple interface to string which has a public method to string as well now i'm going to create a class user and i'm going to implement the user from tostring as well as json serializable interfaces okay then i'm going to actually implement the methods right here all these methods which are necessary for inside that interfaces and then i'm going to create a function to string and i'm going to actually call this with json for example okay and the idea is that i'm going to call that tostring with json i'm going to give it an instance of the user and i want to make sure that whenever whatever variable comes right here that variable must be an instance of both tostring interface and json serializable interface remember from php 8.0 we have possibility to have union types for example you can define that the user can either be an instance of tostring or json serializable so that pipe basically means or however in php 8.1 we have possibility to use intersection types with symbol end okay and just like this if i pass anything other than an instance of both tostring and json serializable basically php will give me an error so if i save this and have a look in the browser we're gonna get nothing which means everything works fine however if i just remove the tostring implementation from here save and reload i'm gonna get fatal error right here to string with json argument one user must be of type to string and json serializable user given and blah blah blah so i i think you get the idea okay that's intersection types all right moving on and next feature is first class callable syntax all right previously before php 8.1 we had possibility to create closures from callable for example these uc words so i can actually create closure and later i can call give it lowercase words hello world and finally i'm going to actually print this value which will give me h and w in uppercase so i'm going to save this and reload and i see right here hello world however in php 8 8.1 we have possibility much easier possibilities i think syntactic sugar on top of that closure from callable so basically i'm going to use uc words right here giving three dots and i'm going to assign it into closure all right i'm going to comment this line because that one basically is equivalent of this and finally i'm going to get the same result however let's check this in php 8.0 we haven't checked many things so far so we're going to get parse error in most cases what i'm showing right now here we're going to get parse error syntax errors because php 8.0 interpreter does not support all these things all right moving forward and talking about next exciting feature which is array is list function that's actually minor feature but i think it's very handy so right here i have numbers and that numbers are basically a plain array without any keys right there any specific keys the keys are basically its own built-in keys from zero to the length of the array minus one and there is new method in php 8.1 which is called array is list that accepts an array and in this case we're going to pass numbers and simply it's going to print true or false if this array is a list or not and again list is array which has keys starting from zero up to length minus one so in this case this is actually a list so this finally will give us true let's have a look save and here is true however in php 8.0 we're going to get encode error call to undefined function arrays list all right let's create let's put some value right here like name corresponds to john and now right here we get false because that's not list anymore okay you get the idea all right moving on and next feature i want to talk is final class constants basically when you define a constant that constant can be overridden in a subclass for example i have a math class right here and i define constant pi 3.14 i have a child class and without overriding basically if i print subclass pi and have a look in php that works fine in php 8.0 as well and 8.1 we get 3.14 however i can use final keyword on that constant i don't want this pi constant to be overridden okay so that's going to work fine however if i try to override public constant pi equals three so first of all my early access page for storm gives me a red underline right here and it tells cannot overwrite final constant pi from math all right let's have a look in php 8.0 first we're going to get cannot use final as constant modifier however in php 8.1 we're gonna get subclass pi cannot override final constant that's pretty like pretty good explanation all right moving on on next feature and that's new in initializers i actually really love this feature because i wish that would be added much earlier okay imagine the following case that we have user and card classes and whenever we initialize user if we just don't provide card class then we should create new card class for that user so how would you do that so these are promoted properties and right now if i just pass right here john and let's actually print that user save and that actually prints in both in php 8.0 and 8.1 and cart is not however before 8.1 php 8.1 i had to write something like this card um question mark question mark equals new cart so if a card is basically now then i'm assigning new card to it and that also works fine in both php versions however i can use new card immediately right here in the constructor or in any method so i can remove that and now let's have a look in php 8.0 i'm going to get constant expression contains invalid operations okay however in php 8.1 this works fine and we get the same output all right moving on the next feature which is called named arguments after unpacking all right right here i have a simple function sum which accepts three arguments a b and c and then down below i'm going to actually call that sum function and i want to pass a and b as an unpacking result of an array i'm going to create numbers array which has one and two inside and i want to unpack that using three dot notation and after this now i can pass c as a named argument corresponds to three so that's new addition in php 8.1 if i just save this and have a look in the browser let's first check in php 8.0 we don't actually see anything what did i do wrong in index.php oops i need to include that named argument save that and reload and write uic cannot combine named arguments and argument unpacking however in php 8.1 we simply see six that's small edition but but i think in certain cases it might be very useful all right last but not the least i want to show you is explicit octal integer literal notation which has very long name but it's super simple to show you all right so if you want to define numbers in base 16 or base 2 you can use 0x for base 16 0 0b for base 2. for base octal base 8 you can use right now before php 8.1 you could use 0 11. and if we just print right here 0 11 if that's in base uh 8 if that is the same as number 9 in decimal and have a look in php both in php 8.0 and 8.1 we're going to get true in both however in php 8.1 it it supports to put right here all okay now if we have a look in the php 8.0 we're going to get parse error in php 8.1 we simply get true so basically now you can to basically this is great to standardize because that's supported by other languages and now it's supported in php as well so put o right here to define numbers in base 8. i will put several useful links in the video description and if you want to know more about what actually has been changed in php 8.1 please check out the following links on the following link you can find how you can migrate your php 8.0 application into php 8.1 here you can see all new features new classes new functions and so on i'm going to actually touch a little bit about backward incompatible changes and right here we see that there has been made some changes regarding how you can actually access these globals variable okay you can directly uh you cannot directly modify that globals variable since php 8.1 you can put the values right there and down below we're going to see more examples probably about that i'm going to again put several links which will show you actual examples regarding that globals or any other things but you cannot actually directly modify globals you cannot use array pop on globals for example there has been made also some changes regarding how you use static variables in inherited methods so check out the following information one of the most interesting and probably incompatible change is resource to object migration now the following functions like f info and when working with basically resources like ftp or ldap or imap or something like this basically they are not returning resources anymore instead they are returning objects of the following corresponding classes another exciting feature which is the most tricky in my opinion are fibers so fibers basically are used to improve parallel processing in php and i probably create a dedicated video for that just let me know in the comment section if you want to see that however the it's written also that fiber should not be used you won't basically need to use it directly instead they are going to be used uh libraries like react php behind so that you can use parallel processes in php which is i think very exciting there existed actually parallel processes before php 8.1 using yelp for example but fibers definitely improve that all right that's the end of this video i hope you like it i hope you learned something new if so um just hit the like button and let me know in the comment section if i uh just missed something you wanted to uh see in this video i'm gonna again put the links in the description and if you're just new to my channel please subscribe if you like the video share it and help me to grow thanks for watching and see you in the next time
Info
Channel: The Codeholic
Views: 17,550
Rating: undefined out of 5
Keywords: TheCodeholic, php, php 8.1, what's new in php 8.1, laravel news, php features, php 8.1 new features, php tutorial, php 2021, php 8.1 tutorial 2021, curso de php, curso de php 7, learn php, coding, php8.1, what is new in php8.1, php8.1 features, php8.1 new, php8.1 jit, php 8.1 tutorial, php 8.1 performance, php 8.1 vs php7, php8.1 named arguments, php8.1 new features, php enums, php8.1 enums, php readonly properties, php fibers, new in php8.1, php8.1 vs php8.0
Id: RaVfeBfUlkk
Channel Id: undefined
Length: 31min 1sec (1861 seconds)
Published: Fri Nov 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.