Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

t0berius's avatar

PHP enum default

is there a way to include a default value (f.e. for non defined values in the enum class):

<?php

namespace App\Enums;

enum ShipmentStatus: int
{
  case PLACED = 0;
  case AWAITING_SHIPMENT = 1;
  case PROCESSED = 2;
}

For example when a value of 5 is received to throw a default case.

0 likes
5 replies
jlrdw's avatar

A ternary to check if greater than 2, if so make it whatever default you need. You may need a better default than one of those choices, just suggestion. Unless placed is the default you need.

t0berius's avatar

@jlrdw

Can you provide an example on how to use it inside the case statement.

t0berius's avatar

@jlrdw no it's not a problem from the user side. There are much more ShippingStatus entries then shown above.

I'm implementing them one by one, so I just want to make sure the application won't crash until all are implemented.

Because of this I'm looking on how to set a default value inside the enum class.

jlrdw's avatar

@t0berius It would be easier to set a default value in a migration. Meanwhile you should still validate what you have so far.

Please or to participate in this conversation.