Type Declarations7:29 with Alena Holligan
PHP is considered to be a weak typed language. In essence, this means that PHP does not require you to declare data types. Variables still have data types associated with them but you can do radical things like adding a string to an integer without resulting in an error. Type declarations can help you define what should occur so that you get the expected results.
The first new feature I want to cover is type declarations. 0:00 This simply means specifying which type of variable is being set, 0:03 instead of allowing PHP to set this automatically. 0:07 PHP is considered a weak typed language. 0:10 In essence, this means that PHP does not require that you declare the data types. 0:14 Variables still have data types associated with them, but you can do radical things, 0:19 like adding a string to an integer without resulting in an error. 0:25 Type declarations can help you define what should occur, so 0:29 that you get the expected results. 0:33 This can also make your code easier to read. 0:35 We'll look at some specific examples shortly. 0:37 Since PHP5, you can use type hinting to specify the expected data types 0:40 of an argument in a function declaration, but only in the declaration. 0:46 When you call the function, PHP will check whether or 0:51 not the arguments are of the specified type. 0:54 If not, the runtime will raise an error and the execution will be halted. 0:57 Besides only being used in function declarations, 1:02 we are also limited to basically two types, a class name or an array. 1:06 Here's an example. 1:12 If we were to create a function for enrolling students, 1:14 we would require that the first argument be an object of the class, Student, and 1:17 the second argument to be an array of classes. 1:22 If we tried to pass just the name of the student instead of an object, 1:25 we would get a fatal error. 1:29 If we were to pass a single class instead of an array of classes, 1:31 we would also get an error. 1:35 We are required to pass a student object and an array. 1:38 If we were to try to check for a scalar variable, such as a string, PHP5 expects 1:43 it to be an object of the class, string, not the variable type, string. 1:49 With PHP7, we now have added scalar types. 1:55 Specifically, int, float, string, and bool. 1:59 By adding scalar type hints and enabling strict requirements, it is hoped 2:04 that more correct and self-documenting PHP programs can be written. 2:08 It also gives you more control over your code and can make the code easier to read. 2:13 By default, scalar type declarations are non-strict, which means they attempt 2:18 to change the original type to match the type specified in the type declaration. 2:24 In other words, if you pass a string that starts with a number into a function that 2:29 requires a float, it will grab the number from the beginning and 2:34 remove everything else. 2:39 Passing a float into a function that requires an int will become simply an int. 2:40 Let's look at an example of a non-strict type. 2:46 The getTotal function receives two floats and adds them together, 2:49 while it returns the sum. 2:53 Without strict types turned on, PHP attempts to cast or 2:55 to change these arguments to match the type specified in the function. 2:59 So when we call getTotal with non-strict types using an int of two and 3:03 a string of one week, PHP converts these to floats, the first argument 3:08 would be changed to 2.0, and the second argument would be changed to 1.0. 3:14 However, you will get a notice because this is not a well formed numeric value. 3:19 The function will then return a value of three, 3:23 which would be completely wrong if we were trying to add days. 3:26 When we call getTotal with the float 2.8 and the string of 3.2, 3:30 PHP converts the string to the float 3.2 with no notice, 3:35 because it was a smooth conversion. 3:39 It then returns a value of six. 3:43 When we call getTotal with non-strict types using the float 2.5, 3:45 and the integer 1, the integer gets converted to the float 1.0 and 3:51 the function returns 3.5. 3:56 Additionally, PHP7 gives us the opportunity to enable strict mode 3:58 on a file-by-file basis. 4:03 We do this by declaring strict types at the top of any given file. 4:06 This must be the very first line, even before name spaces. 4:10 Declaring strict typing will ensure that any function call made in that file 4:14 strictly adheres to the type specified. 4:19 Strict is determined by the file in which the call to the function is made, 4:22 not the file in which the function is defined. 4:27 If a type declaration mismatch occurs, a fatal error is thrown, and 4:29 we know that something is not functioning as desired. 4:34 Instead of allowing PHP to simply guess at what we want to happen, 4:36 allowing PHP to guess can cause seemingly random and hard to diagnose issues. 4:40 We'll look at catching and handling errors in the next section. 4:46 But for now, let's take a look at examples using strict types turned on. 4:49 When declare strict type has been turned on, 4:53 the first two calls that pass a string will produce a fatal error. 4:56 The exception to strict typing is shown in the third call. 5:00 If you pass an int as an argument that is looking for 5:03 a float, PHP will perform what is called widening, 5:07 by adding a dot zero to the end, and the function returns 3.5. 5:11 PHP7 also supports return type declarations, 5:16 which support all the same types as arguments. 5:19 To specify the return type, we add a colon, and then the type, 5:23 right before the opening curly bracket. 5:26 If we specify the return type of float, it will work exactly like it has been working 5:29 in the previous two examples, since the type being returned was already a float. 5:34 Adding the return types allows you to be sure that your function returns what 5:39 you expected to return, 5:44 as well as make int easy to see up front how the function works. 5:45 If we specify the return type as int without strict type set, 5:50 everything will work the same as it did without a return type. 5:54 The only difference is that it will force the return to be an int. 5:58 In the third call, the return value will truncate to three 6:01 because the floating point will be dropped. 6:05 If we turn strict types on, we'll get a fatal type error. 6:09 In this case, we'll need to specifically cast our return value as an int. 6:13 This will then return the truncated value. 6:18 The new type declarations can make code easier to read and 6:21 forces things to be used the way they were intended. 6:24 Some people prefer to use unit testing to check for intended use instead. 6:28 Having automated tests for your code is highly recommended, but 6:33 you can use both unit tests and type declarations working in harmony together. 6:36 Either way, PHP does not require that you declare types, but 6:41 it can definitely make code easier to read. 6:45 You can see right at the start of a function what is required and 6:49 what is returned. 6:52 When you are working with declared types with strict types turned on, 6:54 here are a few things to remember. 6:57 Strict types are enabled or disabled per file, not a php.ini setting. 7:00 Strict applies to the file in which the call to the function is made, 7:07 not the file in which the function is defined. 7:11 If strict is defined in test.php, 7:15 all function calls made from test.php have strict typing. 7:17 Integers will be widened into floats by adding dot zero. 7:22 All other type conversions will error. 7:26
You need to sign up for Treehouse in order to download course files.Sign up