Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

The true type in PHP 8.2

October 18, 2022 ·

The latest and greatest version of PHP, PHP 8.2 is going to be released soon and it’s going to bring a lot of new features and improvements. In this post, I’ll talk about one of the new features in PHP 8.2 which is the true type.

So, you might have stumbled upon the scenario where you want to accept only true as a value for a function parameter. For example, you want to accept only true as a value for a function parameter that is responsible for enabling a feature. In such scenarios, a bool type won’t be enough as it will also accept false as a value. So, you need a type that accepts only true as a value.

That’s where the true type comes into play. It’s a new type in PHP 8.2 that accepts only true as a value. Let’s see how it works with an example.

The true type

As per this RFC, the true type is a new type in PHP 8.2 that accepts only true as a value. So, if you want to accept only true as a value, let’s say, for a function parameter, you can use the true type.

Here’s how you can use it.

function enableFeature(true $feature): true
{
    // do something
}

As you can tell, we can use the true type to accept only true as a value for the $feature parameter. If we pass anything other than true to the function, it will throw a TypeError exception.

We can also use the true type as a return type. For example, if we want to return true from a function, we can use the true type as a return type.

The true type can also be used as a property type. For example, if we want to accept only true as a value for a property, we can use the true type.

class User
{
    public true $feature;
}

The true type also respects Lisko Substitution Principle. So, if we have a function that accepts a bool type, we can pass a true type to it like so.

function enableFeature(bool $feature): bool
{
    // do something
}

enableFeature(true); // works fine

Limitations

The true type is a new type in PHP 8.2. So, it’s not available in PHP 8.1 or below. Also, you can’t use the true type as a union type with other types. For example, you can’t use the true type as a union type with the bool type.

function enableFeature(true|bool $feature): true|bool
{
    // do something
}

This will throw the following error.

Fatal error: Duplicate type true is redundant in %s on line %d

In conclusion

I think the true type is a nice addition to PHP 8.2. It’s going to be very useful in scenarios where you want to accept only true as a value for a function parameter. So, I’m looking forward to using it in my projects.

If you want to learn more about the true type, you can check out the RFC for it.

Learn the fundamentals of PHP 8 (includes 8.1 and 8.2), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-read guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

👋 Hi there! I'm Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I'd highly appreciate that. Cheers!

Comments?