PHP Type Declarations: make your PHP code easier to read, and simpler to use

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
php is a dynamically typed language this means that variables don't have a fixed type however php does have an optional strong type system in this video we'll see how using this will make your code simpler easier to read and easier to maintain before we get into how to use the type system let's start by looking at how types work in php php is a dynamically typed language this means that we don't have to specify the data type of a variable instead php determines the type based on its content in addition to this php also does automatic type conversion where necessary this is known as type coercion or type juggling let's use php's interactive mode to look at an example of this let's create a variable called amount and assign it an integer value of three if we use the get type function to print out its type we can see that it's an integer let's now add a float value of 1.99 to this using the addition assignment operator the variable now contains 4.99 now if we print out the type of the variable we can see it's been automatically converted to a float for historical reasons the get type function calls float values doubles the full list of how different types are type juggled like this is in the documentation let's look at this in the context of a function consider this code here we have a function called add which has two arguments a and b the body of the function adds these two values together and returns the result after the function let's call it with arguments of two and two to see what this returns we'll use var dump which will print out the value and its type let's run this script on the command line and as expected when adding two and two together we get an integer value of four let's try this with some different values before we do that let's add some code to the function to print out the type of each argument which we can get using the get type function and will print a new line character at the end of each line so the output is easy to read if we run that again on the command line we can see that both arguments are integers let's change the second argument to 2.5 a float value if we run this again we see the second argument is recognized as a double or float and the result is also a float let's try that with a string and the second argument is recognized as such with the result being an integer here the string has been type juggled into an integer to provide an integer result this worked because the string contained a number let's try it with a string that isn't a number and now we get a warning when we try and add a non-numeric string to an integer this happens because php couldn't coerce this string value into an integer now let's add some type declarations to these function arguments these are entirely optional but they help to ensure that the value of an argument is of the specified type let's declare that both these arguments must be integers which we do by putting the int type declaration before each argument let's call this with two integers when we run this as before the two arguments are integers and we get an integer result nothing has changed let's change the second argument to 2.5 again when we run this before the result was the float value 4.5 now when we run it the second argument is shown to be an integer and the result is the integer value 4. what's happened is the int type declaration makes php convert this argument to an integer using type juggling so the float value 2.5 was converted to the integer value 2 and the result is four let's try a string argument again when we run this again the argument is shown to be an integer as php has type juggled the string to an integer if we try a string argument that doesn't contain a number when we run it instead of a warning we now get a fatal error specifically a type error the error description is very specific the second argument passed to the add method must be an integer but instead a string was given using a type declaration tells us exactly what the problem is in our code we can do more than this just by adding type declarations like this php will use type juggling to try and coerce the argument values to the specified type we can change this to be more strict by turning strict type mode on we do this with a declare statement at the top setting the strict types value to 1. this must be the first line of the script even before a namespace let's try this again with two integers this works as before with no problems now let's try it with a float argument before we turn strict types on this value was coerced to the integer value of 2 and the result was four with strict types on now we get a type error with strict types enabled we effectively turn type juggling off and require the arguments to be of the specified type when using type declarations we can use scalar values such as integers floats and strings but also classes interfaces and types like self and parent a full list of available types is in the official documentation in addition to function arguments we can also add type declarations to return values and class properties for example consider this class we can add type declarations to the method argument and to its return value by adding a colon at the end followed by the type declaration we can also add type declarations to the class properties in php 8 union types were added so a type declaration can be one or more types separating them with a pipe character you can also make a type declaration nullable that is in addition to the specified type a null value is also accepted by prefixing it with a question mark as for enabling strict mode to maintain backwards compatibility strict type checking is optional it's enabled on a file by file basis by adding this declare line to the top as we just saw so type declarations serve two basic purposes first they enforce the correct type for arguments return values and class properties this means it's more difficult to misuse methods and we have to write less type checking code second it makes the code easier to read it's clearer what a method accepts and what it will return this makes your code easier to maintain and less prone to bugs all the code shown in this video is linked to in the description and if you'd like to see more videos like this please consider subscribing to my channel as always thank you for watching and i'll see you in the next video
Info
Channel: Dave Hollingworth
Views: 4,710
Rating: undefined out of 5
Keywords:
Id: Ig0NbYTStxo
Channel Id: undefined
Length: 10min 53sec (653 seconds)
Published: Sat Feb 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.