|
1 |
| -You have been given a piece of code (look for `the-return-of-static.php` in your working directory) which is using static return types. |
| 1 | +Write a program that accepts two floating point numbers as arguments. The first number is the dividend, and the second is the divisor. The divisor may be zero. |
2 | 2 |
|
3 |
| -You will find two classes. `File`, a base class, and `Image` a class extending and adding behavior to `File`. We instantiate `Image`, set some properties using a fluent interface and then dump the object using `var_dump`. |
| 3 | +First, divide the numbers using the traditional binary operator (`/`). Make sure to wrap it in a try/catch statement because the operator can throw a `DivisionByZeroError` error. |
4 | 4 |
|
5 |
| -If you run the code using `{appname} run the-return-of-static.php` you will see it is broken. |
| 5 | +In the case of an exception, you should print the exception message followed by a new line. |
6 | 6 |
|
7 |
| -Locate and fix the issue! |
| 7 | +Second, use the `fdiv` function with the same numbers. There are a few different return values you can expect from `fdiv` based on the values you pass to it. |
8 | 8 |
|
9 |
| -### The advantages of the static return type |
| 9 | +Based on those values you should print a specific message followed by a new line: |
10 | 10 |
|
11 |
| -* Enforces that an instance of the class the method is called from, is returned. |
12 |
| -* Most useful for fluent interfaces and static constructors to ensure an instance of a parent class is not returned. |
| 11 | +* INF -> print "Infinite" |
| 12 | +* -INF -> print "Minus infinite" |
| 13 | +* A valid float -> print the number, rounding it to three decimal places. |
| 14 | + |
| 15 | +`fdiv` will return INF (which is a PHP constant) if you attempt to divide a positive number by zero. |
| 16 | +`fdiv` will return -INF if you attempt to divide a negative number by zero. |
| 17 | +`fdiv` will return a valid float if your divisor is greater than zero. |
| 18 | + |
| 19 | + |
| 20 | +### The advantages of the fdiv function |
| 21 | + |
| 22 | +* No exceptions are thrown if you divide by zero |
13 | 23 |
|
14 | 24 | ----------------------------------------------------------------------
|
15 | 25 | ## HINTS
|
16 | 26 |
|
17 |
| -(Brief) Documentation on the static return type feature can be found by pointing your browser here: |
18 |
| -[https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.static]() |
19 |
| - |
20 |
| -The static return type enforces methods to return an instance of the class that the method was called from, rather than the one it was defined in. |
| 27 | +Documentation on the fdiv function can be found by pointing your browser here: |
| 28 | +[https://www.php.net/manual/en/function.fdiv.php]() |
21 | 29 |
|
0 commit comments