- Publisher Subscriber Model
- Singleton Pattern
- Use yoda condition checks, you must
Publisher Subscriber Model

The WordPress hooks loosely follow this architecture, When it used the do_action() function, it is publishing an event to which anyone can subscribe using add_action().
The hooks system in WordPress is the base of the whole architecture as it allows us to hook into the WordPress events and extend them, the filters even allow us to modify the data which is really cool
Singleton Pattern
We are using OOP to code our plugin, the other way is to do it in functional way. Functional programming may seem convinient at start but as the code base becomes larger and larger it is difficult to maintain all those functions and finding unique names for them.
The main plugin file we used singleton pattern as it will be used only once and no need to create its instances again and again.
It makes sure that when we are taking instance of the main class, we are getting the same instance every time.
I will make sure that to use this only once at the entry point of plugin, as overusing the singleton pattern is not a good idea
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
require_once 'includes/Movie.php';
require_once 'includes/Person.php';
/**
* Plugin Name: Movie Library Plugin
* Description: Plugin for movie library
* Plugin URI: tr.rt.gw
* Author: Pratik
* Text Domain: movie-library
*/
class MovieLibraryPlugin {
private static $instance = null;
/**
* Private Constructor to implement Singleton pattern
*/
private function __construct() {
register_activation_hook( __FILE__, array( __CLASS__, 'activate' ) );
register_deactivation_hook( __FILE__, array( __CLASS__, 'deactivate' ) );
/**
* Register the custom post types and taxonomies under rt-movie
*/
$movie = new Movie();
$movie->register();
/**
* Register the custom post types and taxonomies under Person
*/
$person = new Person();
$person->register();
}
/**
* Get the instance of the class
*
* @return MovieLibraryPlugin
*/
public static function get_instance(): MovieLibraryPlugin {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Do tasks when activating the plugin
*
* @return void
*/
public static function activate() {
// Functionality to activate the plugin goes here.
flush_rewrite_rules();
}
/**
* Do tasks when deactivating the plugin
*
* @return void
*/
public static function deactivate() {
// Functionality to deactivate the plugin goes here.
}
public static function uninstall() {
// Clean up the plugin
}
}
MovieLibraryPlugin::get_instance();
Use yoda condition checks, you must
The Yoda conditions programming style (or “Yoda notation”) inverts the variable and constant, literal, or function in an equality conditional expression.
This how we typically write conditions :
public static function get_instance(): MovieLibraryPlugin {
if ( self::$instance === null ) {
self::$instance = new self();
}
return self::$instance;
}
Using Yoda conditions
public static function get_instance(): MovieLibraryPlugin {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
This was really helpful for me as I sometimes write my if condition like
if ( $message = null)this will assign null to the $message instead of using comparison
Leave a Reply