PHP 7 has a concept called Type Errors. These errors are thrown on a type mismatch when interacting with functions. They can be caught just like exceptions.
I created a video that explains the situation. This blog post goes into more detail and has some code examples.
But check out the video to get a quick overview.
Type mismatch and type hinting
A type mismatch happens when you expect an input parameter or a return type of a function to be a certain type, whereas in reality you get a different type. PHP offers type hints to indicate what type an input parameter should have.
In PHP 5 the following type hints were available:
In PHP 7 scalar type hints were introduced. This means that the following types can also be hinted in PHP 7:
Let’s say you want your function to accept an integer as an input parameter and you pass a string, there’s a possibility that a TypeError is thrown.
Throwing type errors
I mentioned that a TypeError can be thrown be PHP, just like exceptions. Just so you know: TypeErrors inherit from the general purpose Error class.
As of PHP 7, both Errors and Exceptions implement the more primitive Throwable interface. The Throwable interface describes all the typical methods that we know from Exceptions. Methods like:
That means you can either catch a type error as:
- An actual type error
- A general purpose error
- A throwable
Check out the example below:
When are type errors thrown?
PHP 7 throws type errors when a type mismatch occurs on input or output, but only under certain specific circumstances:
- When the mismatch cannot be coerced to the desired type
- When strict types are enabled, regardless of the possibility to coerce
Under all other circumstances, nothing much will happen and the script execution will continue.
What is strict types all about?
You noticed that I mentioned the term strict types. That’s actually a setting you can enable and disable in PHP.
By default, PHP is loosely typed and the PHP core development team wants to keep it that way. Historically, PHP has achieved its status and its popularity by being loosely typed. This lowered the barrier for people to come in and use PHP as their scripting/development language.
As the community and the language became more mature, the need to have strict types emerged. In order not to conflict with the desire to still have a loosely typed language, a new setting was introduced: strict_types. By default it’s turned off, but you can enable and disable it explicitly.
You can enable it as follows:
When enabled, every type error, even the ones the can be ignored due to type coercion, will the thrown.
Here’s an example of a type error that is thrown due to strict types:
When strict types is disabled, type mismatches are dealt with quite leniently. When PHP notices that the type doesn’t match but can be coerced into the desired type, it will do so without spitting out a type error:
- A numeric string can be turned into an integer
- An integer can be converted to a string
- Bool can be turned into integers and vice-versa
Here’s an example of type juggling where a stringed is converted to an integer without a type error being thrown:
The same deal applies to return types: PHP will try to convert the returned argument to the type that was hinted. Scalar types can usually be coerced.
Here’s an example of type juggling with return types. The integer is turned into a string as instructed by the return type declaration:
Type coercion failures that do throw type errors
As mentioned in the previous section: scalar types can easily be interchanged and often don’t cause type errors to be thrown on type mismatches. When a coercion fails, a type error is thrown.
Here’s an example of a type mismatch that does throw a type error:
And of course, type errors are thrown when function returns cannot be converted into the hinted type: