How to deal with global accessible information without creating a singleton class
I am trying to walk away from the Singleton (anti-)pattern when it comes to develop plugins for WordPress.
But I still want to have an object with properties where multiple classes along different plugins can write and read those properties.
And I sincerely do not know how to approach this.
Can you please let me know how you would do it?
There may be different cases with different solutions, so I will suppose a real example.
Example:
A plugin called “Example Form” creates a custom form on the WordPress site.
The form is processed via a method added to the init hook by the plugin class.
public static function setup_actions_and_filters(){
$this_class = new self();
add_action('init', [$this_class, 'process_form']);
....
I don’t have access to that instance of the class because I called a satic setup method in the main plugin file.
PLUGINCLASS::setup_actions_and_filters();
Did I come up with my own ideas?
Yes.
I thought about:
- Using the database – Horrible idea
- Using cookies – Terrible idea
- Using sessions – Not so nice
- Using globals – I have read many times that it’s not a good idea to use them
Leave an answer