PHP Enums

PHP Enums

An enumeration type, "enum" for short, is a data type to categorise named values. Enums can be used instead of hard coded strings to represent, for example, the status of a blog post in a structured and typed way.

PHP doesn't have a native enum type. It offers a very basic SPL implementation, but this really doesn't cut the chase.

There's a popular package written by Matthieu Napoli called myclabs/php-enum. It's a package I and many others have been using in countless projects. It's really awesome.

Today I want to explore some of the difficulties we encounter when solving problems like enums in userland. I'll talk about my personal take on enums, and we'll ponder on core support.

One last note: I will assume that you know what enums are, and that you know on how to use them in real life projects.

We could write something like this in PHP…

class Post
{
    public function setStatus(PostStatus $status): void
    {
        $this->status = $status;
    }
}

… and be sure that the value of Post::$status is always one of three strings: draftpublished or archived.

Say we'd save this Post in a database, its status would automatically be represented as a string.

The myclabs/php-enum package allows us to write this:

class PostStatus extends Enum
{
    const DRAFT = 'draft';
    const PUBLISHED = 'published';
    const ARCHIVED = 'archived';
}

We could use the constant values directly like so:

class Post
{
    public function setStatus(string $status): void
    {
        $this->status = $status;
    }
}

// …

$post->setStatus(PostStatus::DRAFT);

But this prevents us to do proper type checking, as every string could be passed to Post::setStatus().

A better approach is to use a little magic introduced by the library:

class PostStatus extends Enum
{
    private const DRAFT = 'draft';
    private const PUBLISHED = 'published';
    private const ARCHIVED = 'archived';
}

$post->setStatus(PostStatus::DRAFT());

Using the magic method __callStatic() underneath, an object of the class PostStatus is constructed, with the 'draft' value in it.

Now we can type check for PostStatus and ensure the input is one of the three things defined by the "enum".

Here's the problem with the myclabs/php-enum package though: by relying on __callStatic(), we lose static analysis benefits like auto completion and refactoring:

php7 enum

As you can see in this case, your IDE is unaware of the PostsStatus::DRAFT() method.

Luckily, this problem is solvable with docblock type hints:

/**
 * @method static self DRAFT()
 * @method static self PUBLISHED()
 * @method static self ARCHIVED()
 */
class PostStatus extends Enum
{
    private const DRAFT = 'draft';
    private const PUBLISHED = 'published';
    private const ARCHIVED = 'archived';
}

$post->setStatus(PostStatus::DRAFT());

But now we're in trouble when refactoring an enum's value. Say we want to rename DRAFT to NEW:

Also we're maintaining duplicate code: there's the constant values, and the doc blocks.

At this point it's time to stop and think. In an ideal world, we'd have built-in enums in PHP:

enum PostStatus {
    DRAFT, PUBLISHED, ARCHIVED;
}

Since that's not the case right now, we're stuck with userland implementations.

Extending PHP's type system in userland most likely means two things: magic and reflection.

If we're already relying on these two elements, why not go full-out and make our lives as simple as possible?

Here's how I write enums today:

/**
 * @method static self DRAFT()
 * @method static self PUBLISHED()
 * @method static self ARCHIVED()
 */
class PostStatus extends Enum
{
}

Opinionated, right? It's less code to maintain though, with more benefits.

know this is far from an ideal situation. It would be amazing to see built-in support for enums in PHP one day. But until then, this has to do.

مصطفی برمشوری

Just me, myself and I, exploring the universe of uknownment. I have a heart of love and a interest of lorem ipsum and mauris neque quam blog. I want to share my world with you.


{{title}}

{{description}}

Wordpress Professional Editorial Board

WordPress Professional Editorial Board is a dashboard to monitor and manage publications on your WordPress sites. You can manage posts for editing, reviewing and publishing on one or more WordPress sites through one integrated dashboard.

How to Design Website Footer to Engage More Visitor

Aside from the main body content, a website includes a header and footer, which serve a particular purpose to help visitors. We believe that website footer design is just as important. First of all let’s answer two vital questions: What is a website footer? What should be in the website footer?

How does a switch learn a MAC address which is not in its lookup table?

Suppose the Switch just started, and it received a frame that contains a destination MAC address for a network device not in its MAC addresses table. What happens then? We describe it briefly.

How to download videos from Linkedin?

Here I show you how to simply download videos from LinkedIn to share it on any other social media. It could be done only by the some simple steps. It does not need to install any tools other than your browser (Chrome or Firefox)

امکانات جدید ویراوب: استفاده از ابزارهای تحلیلی برای بهبود سئو

امکان استفاده از ابزارهای تحلیلی مختلف شامل گوگل آنالیتیک، گوگل تگ منیجر و هات‌جر (hotjar) در صفحات سایت‌هایی که روی سرویس‌های ویراوب۱۲۳ ساخته شده‌اند، فراهم شده است. با این ابزار می‌توان به تحلیل رفتار کاربران، تحلیل مراجعات و ترافیک سایت برای بهبود سئو سایت‌های پرداخت.

الگوی فهرست صفحات بلاگ

auto created content

Questions? Chat with us!Support is online.Support is away.Chat with ViraWeb123 TeamChat with ViraWeb123 Team