Is it possible to define the type of a parameter as enum values? – Php

by
Ali Hasan
composer-php enums signature

The Problem:

In a function, is it possible to specify that a parameter can only take certain predefined values (e.g., from an enum)? How can this be achieved in a language like PHP or Java?

The Solutions:

Solution 1: Use an Enum as typehint or `tryFrom()`

If you know the parameter will always be an instance of an Enum, you can use the Enum as the typehint:

public function getValuesFromType(MyTypeEnum $type)

However, if you don’t know the type of the parameter, you can use tryFrom() static function on the Enum:

public function getValuesFromType(string $type) {
    $myType = MyTypeEnum::tryFrom($type);

    if (!$myType) {
       // handle null state
    }

    // $myType is a valid MyTypeEnum instance
}

tryFrom() will return null if the value is not matching with any case.

Solution 2: Add a function to the Enum for values

You can add a function to the Enum to return the values associated with it:

enum MyTypeEnum
{
    case APPLE = 'apple';
    case BANANA = 'banana';

    public function values() {
        return match($this) {
            self::APPLE => ['red', 'green'],
            self::BANANA => ['yellow', 'brown']
        }
    }
}

Usage:

MyTypeEnum::tryFrom('apple')?->values() // return ['red', 'green']
MyTypeEnum::tryFrom('orange')?->values() // return null
MyTypeEnum::tryFrom('orange')?->values() ?? [] // return []

Solution 2: Using a non-backed enum in conjunction with PHP 8.3 and Symfony 6.3

In PHP 8.3, you can define a non-backed enum by specifying the type of the enum as a string. This allows you to restrict the values that can be passed as parameters to a function.

For example, the following function takes a non-backed enum as a parameter:

public function getValuesFromType(MyTypeEnum $type)

The MyTypeEnum enum can be defined as follows:

<?php

namespace App\Enum;

enum MyTypeEnum:string {
    case CHOICE_ONE = 'ONE';
    case CHOICE_TWO = 'TWO';
}

When you call the getValuesFromType() function, you can pass only the values defined in the enum as the parameter. For example, the following code will work:

$type = MyTypeEnum::CHOICE_ONE;
$values = $this->getValuesFromType($type);

However, the following code will throw an error:

$type = 'THREE';
$values = $this->getValuesFromType($type);

This is because the value ‘THREE’ is not defined in the MyTypeEnum enum.

Q&A

In the parameters of a function, is it possible to specify some enumerated values?

Yes, it is possible by using the typehint of your enum.

How to handle the case when the type passed to the function parameter is not an instance of the Enum type?

You can use the from() or tryFrom() static functions on the Enum type to handle this case.

How to add a function to your enum to return different values based on the case?

You can add a function to your enum by using the public function values() syntax.

Video Explanation:

The following video, titled "What's New In PHP 8.1 - Deprecations & Backward Incompatible ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Let's talk about PHP 8.1, what are some of the new features, backward-incompatible changes & deprecations. SOME OF THE WAYS YOU CAN SUPPORT ...