|
| 1 | +As alluded to earlier in `Unit The Types` PHP's type system has been constantly and consistently growing for a long time. |
| 2 | + |
| 3 | +PHP8 comes with a new type `mixed` which is essentially a union of `array|bool|callable|int|float|object|resource|string|null`. |
| 4 | + |
| 5 | +This means that any value which can be produced in PHP will pass the check. Which is exactly the same as no type check. |
| 6 | + |
| 7 | +I guess you're wondering why you would use it, right? |
| 8 | + |
| 9 | +If anything - it signals intent. Your function really does accept anything. It's not just missing type information. |
| 10 | + |
| 11 | +---------------------------------------------------------------------- |
| 12 | +Create a program which contains one function named `logParameter`. It should have one parameter, and it's type must be `mixed`. |
| 13 | + |
| 14 | +Within your function you should log the type of the parameter that was passed. For this, you can use a new PHP8 function called `get_debug_type`. |
| 15 | + |
| 16 | +This function will give you a string identifier representing the type of any PHP variable. |
| 17 | + |
| 18 | +You should log the parameter to a file named `param.log` next to your PHP script. |
| 19 | + |
| 20 | +We will call this function multiple times when we verify it, so make sure you append to the file instead of overwriting it with each log. |
| 21 | + |
| 22 | +Each log entry should be on a new line and should follow the format: `10:04:06: Got: string` where `10:04:06` is the time: hours, minutes & seconds and where `string` is the type, the result of `get_debug_type`. |
| 23 | + |
| 24 | +The format of the line is very important, including the time. |
| 25 | + |
| 26 | +You do not need to call the function, just define it. We will call it for you, with every PHP value we can think of! |
| 27 | + |
| 28 | +### The advantages of the mixed type |
| 29 | + |
| 30 | +* Allows us to indicate that the type has not been forgotten, it just cannot be specified more precisely. |
| 31 | +* Allows us to move information from phpdoc into function signatures. |
| 32 | +---------------------------------------------------------------------- |
| 33 | +## HINTS |
| 34 | + |
| 35 | +The function you implement must be called `logParameter`. |
| 36 | + |
| 37 | +`file_put_contents` has a handy flag to append to a file instead of creating it anew: `FILE_APPEND`. |
| 38 | + |
| 39 | +We will call your function automatically with a bunch of different types to make sure that it can accept anything. |
| 40 | + |
| 41 | +We will specifically check that you used the `mixed` type. Omitting the type will not pass. |
| 42 | + |
| 43 | +Documentation on the `mixed` type can be found by pointing your browser here: |
| 44 | +[https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.mixed]() |
| 45 | + |
| 46 | +Documentation on `get_debug_type` can be found by pointing your browser here: |
| 47 | +[https://www.php.net/manual/en/function.get-debug-type.php]() |
| 48 | + |
| 49 | +---------------------------------------------------------------------- |
| 50 | +## EXTRA |
| 51 | + |
| 52 | +You might want to delete or empty your log file each time your program starts, otherwise it will grow and grow and the comparison will fail. |
| 53 | + |
| 54 | +Think about the return type of your `adder` function - you could declare it as `void`. |
| 55 | + |
| 56 | +If you are curious how we compare the output of your program when it includes time, we simply check that it matches the format, rather than comparing exactly. More specifically, we use the regex `/\d{2}:\d{2}:\d{2}/`. |
0 commit comments