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.
Labs
Labs are hands-on coding projects that you build along with Tonya as she explains the code, concepts, and thought processes behind it. You can use the labs to further your code knowledge or to use right in your projects. Each lab ties into the Docx to ensure you have the information you need.
Each lab is designed to further your understanding and mastery of code. You learn more about how to think about its construction, quality, maintainability, programmatic and logical thought, and problem-solving. While you may be building a specific thing, Tonya presents the why of it to make it adaptable far beyond that specific implementation, thereby giving you the means to make it your own, in any context.
Fully Qualified Naming
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.
Using Functions from Another Namespace
You can use functions from different namespaces. But you have to be aware of how to bring in that function. Remember, the function has a fully qualified name which includes its namespace. When using that function in another namespace, you have choices: You can specify its fully qualified name such as \KnowTheCode\InsOutsPHPNamespacing\Sandbox\get_post_id() If the namespace is the root, then you can use a relative name such as Sandbox\get_post_id() You can alias the namespace and then use that alias in place of the fully qualified namespace If you are using PHP 5.6 and up, then you can import the function. You […]
The Basics
The best to learn about namespace and to wrap your mind around how it’s different than prefix is to use it and see it in action. In this episode, you will add a function called get_the_ID() to your plugin. Wait a minute. That function exists in WordPress Core. Aha, you’ll see what happens when you use a namespace around the plugin’s function. You’ll see how to call WordPress Core’s function versus the one in your plugin.
Why Namespacing instead of Prefixing?
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. […]
What and Why of Namespacing
What is PHP namespace? Why should you care about using it versus prefixing? What does it give you? What are the advantages? Let’s talk about it. The first thing to know is: the web server must be running at least PHP 5.3.0. This is an old version of PHP. It stopped being supported as of August 2014. It’s old. Shoot, 5.4 and 5.5 are old too and no longer supported. Check out the supported versions here on the PHP manual website. PHP namespace encapsulates your code. What does that mean? It means you are putting a container around your code. […]
Lab Introduction
In this episode, you’ll learn what you will be doing in this lab. We’ll also make sure that your sandbox is ready to go. 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 u
“Object Not Available” Strategies
There are times when you can’t get a hold of the object in order to remove a method callback from that object. The author wrote it such that s/he did not expose the object for you. What do you do? How can you get that object in order to do your work and unregister its callback? Here are strategies in order: Talk to the author and see s/he will add a filter or action to pass the object to you. If it’s open source, submit a pull request to add the filter/action code. Evaluate if you have to use this […]
Wrap it Up
Let’s review what you learned in this lab about registering and unregistering callbacks for both static and object methods. You looked into WordPress Core and saw that it is using call_user_func_array to call each of the registered callbacks for a given event hook. You learned strategies for working with 3rd party code including WooCommerce. If the WordPress Event registrations confused you, go take this lab. Congratulations for completing this lab!
Design Strategy
When you are registering and unregistering callbacks within an object, you typically do this task as part of the object’s initialization process. Instead of putting all of the init tasks into the constructor, break it out. Put the callback registration into a separate method called init_events().