Why Namespacing instead of Prefixing?
Lab: Ins and Outs of PHP Namespace for WordPress
Video Runtime: 12:39
Let’s start by discussing why WordPress is built with prefixing and not namespacing. Namespace did not come to PHP until version 5.3.0. WordPress was built long before PHP v.5.3. Aha! When namespace was added to PHP, it didn’t make sense to re-write WordPress Core. Right? Also, sadly there are still some old servers in the world that run PHP versions below 5.3.
PHP 5.3 is no longer supported as of August 2014. It’s old. 5.4 is no longer supported either as of September 2015. And guess what? Even 5.5 is out-of-date and no longer supported either as of July 2016. Check out the supported versions here on the PHP manual website.
My point is: Namespace came to PHP on 30 June 2009. It’s been a long, long time ago in the world of software. It’s time for us to embrace the new and start building with namespacing and drop prefixing.
Why I Don’t Like Prefixing
The major reason why I don’t like prefixing is because of readability.
Functions should be named to tell you what will happen when you call it. They should tell you the expected behavior. Make them readable to tell you what they do when you invoke them.
The name should always start with a verb, because functions do work. You should be able to read that name and know what it will do for you. Period.
When you add a prefix onto the front of a function’s name, how does that prefix tell you what that function does? It doesn’t. It’s merely there to prevent name collisions. That’s it. It does not serve readability. It does not make your code expressive or tell you what it’s going to do.
Instead, you have to read past the prefix to know what the function does. Anything that makes you stop and figure out how to read it makes you less productive.
What is a Naming Collision?
If you have a class, trait, interface, or function that is named the same as some other code, the website will crash. PHP will throw an error telling you that you can’t redeclare that particular name.
Why? Imagine that you have a function called
activate_plugin(). Now imagine that another plugin uses that exact same name. Which function should PHP run when you invoke it? Hmmmm, it won’t know. Functions (class, trait, interface, and variables) have to have a unique name within its scope. Else, PHP will yell at you.
Why Use Namespace Instead of Prefixing?
Namespacing lets you get more expressive to the intent and context of the code. It makes it more readable, reusable, and maintainable. It greatly reduces the risk of naming collisions. It lets you pre-build modules and then load them into your theme or plugins without having to change the naming.
The syntax and name structure:
PHP Namespacing should follow the code’s architecture, i.e. the folder structure. When you do that, you get further context when reading the code. You know what module or sub-module it belongs to. You know where to find it. You know who wrote it by the company name. You know what module or package it belongs to.
And the namespace separator, i.e. the backslash
\ is easily readable. Your eyes can past over it to get to the class, function, variable, trait, or interface name. You can import and/or alias to make it more readable.
PHP Namespacing is prevalent outside of WordPress. Go check out Laravel, Symfony, CakePHP, Phalcon, Yii 2, Slim, and many others.
If you listen carefully, you can hear the 1s and 0s flowing in your computer.
Total Lab Runtime: 02:25:26
- 1 Lab Introductionfree 02:59
- 2 What and Why of Namespacingfree 20:15
- 3 Why Namespacing instead of Prefixing?free 12:39
- 4 The Basicspro 17:36
- 5 Fully Qualified Namingpro 06:50
- 6 Callbacks and Namespacingpro 08:00
- 7 Name Resolution Rulespro 12:17
- 8 Using Functions from Another Namespacepro 16:37
- 10 Class Basicspro 12:20
- 11 Practical Example: Convert from Prefixingpro 10:08
- 12 Real World Example: Convert from Prefixingpro 06:14
- 13 Architecture - Building in Packages and Modulespro 08:59
- 14 PHP Examples - Laravel & Symfonypro 02:50
- 15 Wrap it Upfree 07:42