How to structure a plugin into multiple files using classes?

Some basic rules for object organization in a plugin.

  • One class declaration per file, no other code in that file. The creation of a new instance – new Dummy_Class – should not be in the same file as the class declaration.

  • Everything that is sending data to the user (presentation) should get its own class: HTML, CSV, XML output or HTTP headers.

  • Every algorithm, every data source should get its own class, so you can change it later. Lets say, you store some data in an option and want change that later to use a custom database table: then you should be able to do that without changing all files.

  • Registration and creation of objects belongs to a separate class (controller).
    add_action( 'something', array( $this, 'callback' ) ) is usually a sign of bad design. Use a separate object instead of $this.

  • Avoid global constants, they clutter up the global namespace. Just pass the path of the current plugin file to the main controller.