It occurred to me that there were strategies I did in the last episode which need further explanation such as: How mb_substr works 05:21 Why did I use mb_substr instead of mb_strpos 08:40 Refactoring to make the code more clear 09:21 How does function_exists() work and why/when do you use it 11:01 Should you use namespacing or function_exists() for helper utilities In this episode, let’s dive into each of these strategies to help you figure out the best approach for you.
In WordPress, we register callbacks to events by using add_action or add_filter. In the last episode, you learned that you must specify the fully qualified name to invoke a function (i.e. run it) outside of the namespace. This applies to add_action or add_filter. In this episode, you will learn why as well as a shorthand version using the magic constant __NAMESPACE__.
Whew, you did it! Congratulations for completing this lab! Let’s review what you learned as well as show you the code here on Know the Code. Remember, namespacing is only available in PHP version 5.3.0 and up. That shouldn’t be a problem for you. Why? Because 5.3 stopped being supported back in 2014. It’s old. Anything older than it can be problematic. Therefore, build your code to be at least 5.3 and up. You can check the PHP supported versions by clicking here.
In this episode, let’s stop and talk about how you architect your code. Code should be built in modules. These modules are not assembled into your plugin or theme. It’s not snippets of code. Rather, they are complete, fully-tested, ready-to-go component or feature modules. All you have to do is change the configuration and none of the codebase. That makes it a reusable module. For example, let’s say you are building a theme. In your code libraries, you have modules for: WooCommerce bbPress LearnDash MemberPress BuddyPress Customizer and more The theme you are building needs WooCommerce, bbPress, and Customizer. You […]
Next, let’s take a real world plugin and convert it from prefixing to namespacing. Let’s convert Tom McFarlin’s Easier Excerpts plugin. You can install this plugin within the WordPress back end by going to Plugins > Add New and then type in Easier Excerpts. In this episode, you will walk through the steps to remove the prefixing, add in the namespacing, and provide better function naming that starts with a verb.
Next, let’s talk about PHP name resolution rules. That means: how PHP figures out what code to run for the given name. If you look at the PHP Manual, they give you a techie write-up that likely will confuse you. Essentially, here’s what you need to know: How you declare the function is the starting point. If you include the fully qualified name, you are telling PHP exactly which function you want; whereas, if you leave off the namespace, then it walks through its process ordering to figure out which function to run. You can name using: Unqualified name: no […]
Let’s put your name skills to use. In this episode, you will convert the supplied plugin from prefixing to PHP namespacing. The best way to learn this is to start where you are, i.e. in prefixing, and walk through step-by-step the process of making a plugin namespaced. You’ll change callbacks, use the magic constant __NAMESPACE__. You’ll remove the namespacing. Step-by-step. Let’s do this together.
Class structures use namespacing too, just like functions do. Within its namespace, PHP knows how to find the methods and properties of a class blueprint. When you use the class outside of the namespace, you need to make PHP know which class you mean. That means you can either: You can specify its fully qualified name such as \KnowTheCode\InsOutsPHPNamespacing\Sandbox\Post If the namespace is the root, then you can use a relative name such as $post = new Sandbox\Post(); You can import the class using the PHP keyword use. You can import and then alias the class. You can learn more […]
As you build your PHP expertise, you will likely grow into other frameworks and applications outside of WordPress. Laravel and Symfony are two awesome frameworks. Many times, you can pull in the modules and packages from their libraries even into a WordPress application. Yes, that’s how well these frameworks are built. Let’s take a quick look at the namespacing in each of these frameworks to show you how it’s used in the wild. You can also take a look at code construction and architecture too, i.e. to further your software building skills.
To help you master the concept of “fully qualified naming,” let’s take a look at what each function’s name is. You will use the magic constant __FUNCTION__. It returns the fully qualified name of the function, which includes the namespace if one exists.