Share
Preview
Let's see how strict typing works and how you can use it.
 ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌
ALEX WEB DEVELOP

Alex
Strict Types: Should You Use Them?
by Alex
Strict Types


Hey,
Alex here.

There is a little known PHP construct named declare.
You can use it to set code directives.

One of these code directives is
strict_types, and today we are going to see how it works and how you can use it.



- What does strict_types do?
To enable strict typing for a PHP script, you need to use the following syntax:

<?php
declare(strict_types=1);


This enables strict typing for all the PHP code of that script that comes after the declare statement.
But what does strict typing do, exactly?

You know that PHP is not a strictly typed language.
However, it lets you specify the required variable types in some contexts.

For instance, you can do that for function arguments and return values, like this:

function average(int $a, int $b): float
{
   return ($a + $b) / 2;
}


But even in these cases, PHP does perform automatic type conversions.
For example, you can call the average() function passing two string arguments instead of two integer arguments, and PHP will automatically convert them into integers:

echo average("3", "2");
// Output: 2.5


With strict typing enabled, PHP forbids this.
You are therefore required to always use the correct types, without relying on PHP automatic conversion.
If you try the above code with strict typing enabled, you get a fatal error:

Fatal error: Uncaught TypeError: average(): Argument #1 ($a) must be of type int, string given...


- How is strict typing applied?
It is important to understand how strict typing is applied by PHP.
When you enable strict types in a script, the strict typing rules are enabled only for the code in that script.

To see what that means, let's say that you have a PHP script with strict types enabled.
In that script you define a function with typed arguments, and you also call a function from another script which has typed arguments as well.

In both function calls, you need to pass the correct argument types.
This applies regardless of where the function definition is (in the current script, or in another script).

In other words, the strict typing rules apply to any function call done in the current script, even if the called function is defined in a different script.

On the other hand, if someone else includes your script and calls your function but does NOT enable strict types in his/her script, then the strict typing rules will not apply.


- Should you enable Strict Types?
Strict typing helps you make your code more precise by preventing some automatic type conversions, which can sometimes provide unexpected results.

But even when type conversion works as expected, strict typing has the advantage of making you more focused on your code, ultimately reducing bugs and making your apps more solid.

That said, enabling strict types can cause problems if your app is not entirely designed with strict typing in mind.
For example, two different functions may take the same value as their arguments, but one function may take it as an integer and the other as a string.
If you enable strict types in a script, then you will be forced to call those functions passing a different type, even for the same value.

Here is my advice.
For already built applications, enabling strict types may not be a good idea and it can cause hard-to-fix errors.

But if you are building a new app from scratch, then you should consider using strict types to make your coding more precise.



That's all for today.
Now send me a reply with your questions and let me know what you think.

Until next time,
Alex



Share the knowledge

Did you like this email? Share it with your friends!
Click here to share it



Premium products to improve your skills

PHP courses

PHP Security Mastery
It would be great to stop worrying about web attacks, right? You know, to be confident that your code is always as secure as it should be.
So, how do you make your PHP code always secure?
You can do that by using the defense techniques that I teach you in this course (with real code examples).

Click here and see for yourself.


Resources

Alex Web Develop - My tutorials.
Alex PHP café
- My Facebook group where to talk with me and other developers.

Abstract vector created by macrovector - www.freepik.com




You are receiving this newsletter because you subscribed to Alex Web Develop.

If you unsubscribe, you will not get any more emails from me.

Alessandro Castellano, P.IVA (VAT ID): 07012140484, via Luigi Morandi 32, 50141 Firenze FI, Italy

Email Marketing by ActiveCampaign