Simple example of using PHP 8.1 enums

Nacho Colomina
2 min readJan 22, 2023

--

Php 8.1 comes with a new great feature called enums. Enums allow us to mantain a collection of constant values. Enums, like classes, can define methods and even implement interfaces.

In this article, I would like to share with you a practical example of how to use enums. If you want to learn more about how enums work there are plenty of documentation in the internet.

I recommend the article : https://medium.com/geekculture/php-8-1-welcome-enumeration-5b9ed1063ec0 where the author explains very well how enums work.

Let’s start with the example. Imagine we want to classify an api call result status from the returned http code. According to the documentation of that api, the http codes returned would be the following:

  • 200: Processed
  • 202: Accepted and will be processed
  • 502 or 504: There are network problems
  • 503: Service (api call) not available
  • 403: Non authorized to perform operation / call
  • 401: Non authenticated

This is only an example. In a real environment it could be different with many other codes and criterias, but as an example is enough.

Now, let’s see how our enum looks like:

enum CallStatus
{
case AUTHENTICATION_FAILED;
case NON_AUTHORIZED;
case OK;
case SERVICE_ERROR;
case NETWORK_ERROR;
case UNKNOWN;

public static function fromStatusCode(int $statusCode): self
{
return match ($statusCode) {
200, 202 => self::OK,
401 => self::AUTHENTICATION_FAILED,
403 => self::NON_AUTHORIZED,
503 => self::SERVICE_ERROR,
502, 504 => self::NETWORK_ERROR,
default => self::UNKNOWN
};
}
}

As we can see, our enum holds 6 cases:

  • AUTHENTICATION_FAILED: Used when api retuns a 401 http code
  • NON_AUTHORIZED: Used when api retuns a 403 http code
  • OK: Used when api retuns a 200 or 202 http code
  • SERVICE_ERROR: Used when api retuns a 503 http code
  • NETWORK_ERROR: Used when api retuns an 502 and 504 http code
  • UNKNOWN: Used for any other code api could return.

In order to instantiate the enum, I’ve created an static method fromStatusCode which takes the http code as a parameter and use the php 8 match operator to compare the case with the http code.

Now , let’s make a unit test so we can ensure enumeration behaves as expected:

final class CallStatusTest extends TestCase
{
public function testOk()
{
$callStatus = CallStatus::fromStatusCode(200);
$this->assertEquals(CallStatus::OK, $callStatus);
}

public function testAuthFail()
{
$callStatus = CallStatus::fromStatusCode(401);
$this->assertEquals(CallStatus::AUTHENTICATION_FAILED, $callStatus);
}

public function testNonAuthorized()
{
$callStatus = CallStatus::fromStatusCode(403);
$this->assertEquals(CallStatus::NON_AUTHORIZED, $callStatus);
}

public function testServiceError()
{
$callStatus = CallStatus::fromStatusCode(503);
$this->assertEquals(CallStatus::SERVICE_ERROR, $callStatus);
}

public function testNetworkError()
{
$callStatus = CallStatus::fromStatusCode(504);
$this->assertEquals(CallStatus::NETWORK_ERROR, $callStatus);
}

public function testUnknown()
{
$callStatus = CallStatus::fromStatusCode(417);
$this->assertEquals(CallStatus::UNKNOWN, $callStatus);
}
}

In the above unit test, we simply check that the enum case matches the http code received as a parameter. If we execute the tests we can see they pass successfully:

Testing call status enumeration

I hope this example can help readers about how enumerations can be used :)

--

--