Author: Manuel Lemos
Viewers: 59
Last month viewers: 5
Categories: PHP Tutorials, News
This PHP 8.2 feature helps PHP developers catch bugs earlier and improve their code quality in practice by allowing them to be more specific in the types returned by functions that may have to be of these three new types: true, false, and null.
Read this short article to learn more about this new PHP 8.2 possibility and how you can use it in practice in your PHP applications to improve their quality.
What Are PHP Standalone Types for true, false and null?
PHP is not a strictly typed language. This means variables may have types like string, integer, float, etc... Functions requiring parameter values of specific types may tolerate developers passing parameters of different values. Internally, PHP converts values of incorrect types when possible.
For instance, the function str_repeat takes an integer value as a second parameter. If you pass a string with a number, PHP will convert the number into a string, as seen in the example below.
$times = "10"; echo str_repeat("Repeat string", $times);
Additionally, PHP does not require that developers explicitly declare the variable types in more strictly typed languages such as Java.
These characteristics make working with PHP more productive, as you need to write less code to perform specific tasks than if you use a more strictly typed language.
On the downside, the lack of type verification allows developers to ship application code to production sites that are not well-tested and may contain subtle bugs. That may increase the risk of causing some damage to the application users.
This fact becomes relevant in applications that may involve delicate aspects of people's lives, like financial or health applications.
Therefore, PHP 7 introduced optional support to enforce the types of variables, function parameters, and return values. This way, developers of those applications can detect bugs earlier in the development phase of a project.
Before version 8.2, PHP supported several basic types like string, boolean, float, and integer.
PHP 8.2 went further and made specific constants like true, false, and null have their types. For instance, true is of type boolean and also type true.
Why PHP Standalone Types for true, false and null Are a Good Thing For PHP Developers
As you may have read above, using strict types in the declaration of functions is helpful to detect PHP bugs earlier and have a more excellent quality of the projects.
If you have a function that can have several values, you may declare it in a way that PHP will validate and accept the values you declared.
Still, in some cases, a function may return values of a particular type, but not all values of that type are acceptable to be returned by the function.
For instance, the PHP function strpos may return an integer or false. Even thought true is a valid boolean value, true is not a valid return value for the strpos function.
PHP 8.2 introduced the possibility to treat true, false and null as values of standalone types. This possibility was proposed in RFC documents for true type and null and false as standalone types.
Therefore the strpos function is declared this way:
strpos(string $haystack, string $needle, int $offset = 0): int|false
So, since PHP 8.2, you can also have your own functions and variables that declare strict types that can be either true, false or null.
How You Can Use PHP Standalone Types for true, false and null in Practice?
You can use the example of the strpos function presented above as an example of what you can do with this new possibility of PHP 8.2.
The RFC document for the true type feature has more examples that are more likely to appear in your PHP applications. Consider for instance, the example below:
abstract class User { function isAdmin(): bool { return false; } }; class Admin extends User { function isAdmin(): true { return true; } }
In this case you have a User class that and an Admin class that implement the isAdmin function. This function can return a boolean value. In the case of the Admin class, the isAdmin function should always return true. PHP 8.2 allows this possibility.
This is a very simple example for you to understand. In real applications, your functions are usually more complex.
If for some reason you made a mistake and the isAdmin function returns any other value that is different from true, PHP would trigger a TypeError exception, as you may see in the altered example code below:
abstract class User { function isAdmin(): bool { return false; } }; class Admin extends User { function isAdmin(): true { return false; } } $admin = new Admin(); $is_admin = $admin->isAdmin();
Here is the output of this example script:
Fatal error: Uncaught TypeError: Admin::isAdmin(): Return value must be of type true, bool returned in true.php:13 Stack trace: #0 true.php(19): Admin->isAdmin() #1 {main} thrown in true.php on line 13
You need to be a registered user or login to post a comment
1,616,107 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
No comments were submitted yet.