Applying OO patterns and principles to plugin development

I have one or two non-class scripts, mostly for forms, that I don’t quite know how to neatly fit into classes.

Split your project into multiple classes. Keep your worker classes as instantiable objects (typical OOP patterns) and put your non-class scripts into a class of their own as static scripts.

I often use static PHP classes to namespace my code this way. Example:

class My_Object {
    public $variable = 1;

    public function __construct() {
        // Constructor
    }

    public function do_something() {
        // Misc. method for the object
        $this->variable++;
    }
}

static class Helper {
    public static function Run_Script() {
        // Function that does something and doesn't belong in a class
    }
}

The difference between these two classes is that you instantiate one (My_Object) and use it as an object. The other is basically a namespace wrapper for a function that doesn’t belong in an object but that also doesn’t belong in the global namespace.

Let’s say you started with just a regular Run_Script() method in your plugin … then later down the road, another developer adds a Run_Script() method to another plugin. You now have a name conflict. Keeping your standalone functions inside a static class helps prevent this.

Just call Helper::Run_Script() to access it.

What resources are there I can consult for guidance on this aspect of my OO plugin?

Basically, look at what others have done. There are quite a few quality plugins out there that follow this kind of a pattern. So take a look at how others have tackled the problem and learn from their example.

GitHub is a great place to look at code. Most of my plugins are up there, so you can browse through code files to view the structure without needing to download them and install them.

Some good developers on GitHub that you should follow for more examples: