PHP Namespace gives you the means to encapsulate your code modules to improve readability and prevent code naming collisions. In this hands-on lab, you will migrate from prefixing convention to PHP namespacing. You will learn the ins and outs of namespacing.
WordPress’ de facto is to use “prefixing” to avoid name collisions. Why? Because WordPress started before namespaces were available in the PHP scripting language. PHP introduced namespacing in version 5.3.0. You can learn more about it in the official PHP documentation.
What Will You Learn?
In this hands-on lab, you and I are going to ease into namespacing. You’ll learn about:
- Why use PHP namespacing
- The problem and potential of naming collisions
- Problems of prefixing
- How namespacing enhances readability, reusability, and maintainability.
- The syntax and placement in files/li>
- The naming conventions
- Naming within a namespace
- Using code from another namespace
- Importing and aliasing
- How to convert a plugin from prefixing to namespacing
- and more
You will learn as you write code, as it’s the best way for you to learn.
#PHP namespace is a better naming convention than prefixing. Come learn about it for #WordPress Share on XPrerequisites
In order to do this lab, you will need a local sandbox site spun up and ready to go. You will need to install the lab’s starter plugin from GitHub, as you will be using it in this lab.
You get WET when you swim. Stay DRY when you code.
Episodes
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