Beyond Prefixing: A WordPress Developer’s Guide to PHP Namespaces

Prefix everything.
It is an adage that is old as the WordPress software itself. Prefixing has been a standard for WordPress developers for so long that it’s hard to imagine doing anything different. But, the time has come for something new. Well, it is long past due, but WordPress lags a bit behind in standard practices in the larger PHP world.
Prefixing is the practice of creating a code-friendly version of your project name and sticking it to the front of functions, classes, and other things in the global namespace. For example, you would name a function tavern_get_post() instead of get_post() to avoid function name clashes, which would result in a fatal error.
Prefixing is one form of “namespacing,” which is just a fancy way of saying that names in this space belong to a specific project. However, prefixing (and suffixing, which is less common) is a hack from a time when no solution existed for the PHP language.
PHP 5.3 introduced an official method of namespacing, so the standard has existed for years. Because WordPress 5.2 bumped the minimum PHP requirement to 5.6, it is time for developers to shed their old habits and catch up to the rest of the PHP world.  » Read More