Database IDs are Magic Numbers

I often come across code where people are using database IDs in a conditional check for something.

Well, guess what? These are magic numbers and magic numbers are not good.

Magic numbers are one of oldest ills in computing. They are numbers with special values that usually are not obvious. Magic numbers are really nasty when you need to reference the same logical number in more than one place. If the numbers might ever change, making the change is a nightmare. Even if you don’t make a change, you have the difficulty of figuring out what is going on.

-- Refactoring: Improving the Design of Existing Code

So, for example, say you have:

if ($user->role->id === 6) {
    // DO something
}

Anybody reading this will have to know what 6 means in order to understand what your code is doing. That's not good.

You could refactor to a symbolic constant similar to this:

if ($user->role->id === Role::ADMIN) {
    // DO something
}

Or do one even better and refactor it to a method:

if ($user->hasRole(Role::ADMIN) {
    // DO something
}

or

if ($user->isAdmin()) {
    // DO something
}