TL;DR: I find the using a registry, subscribers, and services very useful when building backend-centric plugins and utilities for WordPress. This post walks through how to do it.
After working in with design patterns, object-oriented programming, and WordPress for years, common ways of solving problems are bound to arise.
This is how we got object-oriented design patterns to begin with, so maybe this is a WordPress-centric variation of that.
Though I’ve written about things such as registries in previous articles (and ones that are not that old even), it’s never a bad idea to revisit the same topic especially when there’s something to continue to add to the previous take.
TL;DR: If you’re working on a variety of projects each of which requires different versions of PHP, Composer, and/or NPM you may need to change the version of all or any permutation of any of these utilities.
This article outlines what steps need to be taken to downgrade Composer, PHP, or NPM when working on any given project.
$argv is an array of arguments passed to the script with the first index being the name of the script itself. And
$argc is the number of arguments passed to the the script (which will always at least be
Arguably, pun intended, one of the key pieces of command-line application is making sure that they are interactive through command-line arguments. In PHP, there are two variables to understand:
$argc is the number of arguments passed to script. Note the script’s filename is always passed as an argument to the script, therefore the minimum value of
$argv is an array of arguments passed to script. Note the first argument
$argv is always the name that was used to run the script.
TL;DR: I’ve been writing PHP command-line scripts to help automate mundane tasks. As these tasks are growing in complexity or turning more into applications that interface with third-party APIs, I’m documenting the things I find important that others may also find helpful.
There are three things necessary to get a basic script up and running on your local machine (assuming you already have PHP installed):
- Know the path to the PHP binary,
- Make sure your script references said binary,
- And make the script executable.
macOS is still shipping with PHP (though how this looks for future versions of the OS is likely going to change in some way.).
Anyway, imagine you’re in a situation where you’re working on a project that requires three different components:
- A set of coding standards,
- PHP CodeSniffer that work with said coding standards,
- The inability to properly see results of sniffing the code either in your terminal or your IDE.
When this happens, this is almost always a result of a version of PHP, the version of the various dependencies, and making sure they all play well together.
In short, it’s about making sure the tools like PHP CodeSniffer work well with the same version of PHP you have installed. And in this case, the latest and greatest isn’t necessarily the best course of action.
It is, however, close. And in the following steps, you should be able to get everything working exactly as needed for your specific set up.