One of the advantages of working with object-oriented programming is the ability to define interfaces so classes that implement those interfaces have a strict set of functions that said classes must implement.
That is the definition of a class interface, anyway:
An interface is a contract specifying a set of methods, fields and properties which will be available on any implementing object
WordPress Plugin Interfaces: Assets
We’ll need to incorporate the following:
- a namespace that can be used throughout the project,
- a function for initializing the class,
- a function for actually enqueuing the assets.
The end result may look something like this:
Next, we need to actually implement the interface with a class. It should be relatively straightforward since we’ve set the methods that must be implemented.
Notice, however, that I provide a couple of properties that are set in the constructor of the class. These are specific to this class and make the act of actually enqueuing a file a bit easier.
A few things to note about the code above:
- it uses the use keyword so that PHP knows we’ll be working with the Asset interface,
Finally, to instantiate the class that implements the interface, you simply do this:
Sure, this particular example is simple and I’ve left the usual code comments out but that’s generally for the sake of readability. If you want to see something like this in action, then review the source code for
If you want to see something like this in action, then review the source code for this particular plugin.