Messages

Sliders

Sliders

Add a Signup Form to Your Website

Add a Signup Form to Your Website

مراحل ثبت دامنه و تنظیمات DNS

مراحل ثبت دامنه و تنظیمات DNS

چرا ترافیک وب سایت برای تجارت من مهم است؟

چرا ترافیک وب سایت برای تجارت من مهم است؟

Why is Website Traffic Important to My Business?

Why is Website Traffic Important to My Business?

ترافیک وب

ترافیک وب

Web traffic

Web traffic

8 نشانه اساسی گوگل برای رتبه بندی در سال 2019

8 نشانه اساسی گوگل برای رتبه بندی در سال 2019

مشکلات رایج در کدنویسی معتبر و رفع آنها

مشکلات رایج در کدنویسی معتبر و رفع آنها

article حل مشکل محتوای تکراری (Duplicate Content) در سایت

article حل مشکل محتوای تکراری (Duplicate Content) در سایت

رتبه بندی الکسا (Alexa) چیست و چقدر اهمیت دارد؟!

رتبه بندی الکسا (Alexa) چیست و چقدر اهمیت دارد؟!

چرا برخی سایت ها و وبلاگ ها رتبه الکسای بهتری دارند؟

چرا برخی سایت ها و وبلاگ ها رتبه الکسای بهتری دارند؟

article پیج رنک چیست و چگونه می توان آن را افزایش داد؟

article پیج رنک چیست و چگونه می توان آن را افزایش داد؟

article چگونه سایت یا وبلاگ را برای موتورهای جستجو بهینه سازی کنیم؟

article چگونه سایت یا وبلاگ را برای موتورهای جستجو بهینه سازی کنیم؟

کدام نوع وبلاگ می تواند برای تجارت  شما مفید باشد

کدام نوع وبلاگ می تواند برای تجارت شما مفید باشد

What a Blog Can Do For Your Small Business

What a Blog Can Do For Your Small Business

WhatsApp vs WhatsApp Business App: What's the Difference?

WhatsApp vs WhatsApp Business App: What's the Difference?

TinyMCE 5: Inline editor example

TinyMCE 5: Inline editor example

TinyMCE 5 - Inline editor example

TinyMCE 5 - Inline editor example

Native HTML5 Drag and Drop

Native HTML5 Drag and Drop

HTML5 Drag and Drop

HTML5 Drag and Drop

Step Organization in Java

Step Organization in Java

How to use Cucumber? – Cucumber best practices

How to use Cucumber? – Cucumber best practices

A Guide to Cucumber Best Practices

A Guide to Cucumber Best Practices

Why Responsive Design Support is the Most Important Feature You Can Add To Your Website

Why Responsive Design Support is the Most Important Feature You Can Add To Your Website

5 مزایای قدرتمند وبلاگ نویسی برای تجارت شما

5 مزایای قدرتمند وبلاگ نویسی برای تجارت شما

5 POWERFUL BENEFITS OF BLOGGING FOR YOUR SMALL BUSINESS

5 POWERFUL BENEFITS OF BLOGGING FOR YOUR SMALL BUSINESS

Angular 1 using redux architecture

Angular 1 using redux architecture

Home Login Logout

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.

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:

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.

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

Lates posts
Most visited posts

Built with love to give everyone an opportunity to say something to the world
Copyright © 2018 All rights reserved - SITE123 

Terms - Privacy - Abuse

LocalTheme

Help