I am trying to walk away from the Singleton pattern when it comes to develop plugins for WordPress.
Why?
Because I have been reading in some sources (here, here, here, here …) and came up with the conclusion that they are a bad thing. Or at least, not something one should use just because.
Why would I need a Singleton-like behavior?
Because I 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.
And while processing the form I may want to add some error or success messages to the active instance, so I can then display those messages in a theme doing somethig like:
But I don’t have access to the “original” instance because I called a satic setup method in the main plugin file.
And that is why a common solution is to make the PLUGINCLASS use the Singleton pattern, which I want to avoid.
Did I come up with my own ideas?
Yes.
I thought about:
- Hiện tại chúng tôi có hơn 25 nghìn bài văn mẫu các thể loại.
- Kho tài liệu, đề thi học sinh giỏi các lớp vô cùng phong phú.
- Mỗi ngày cập nhật hơn 100 đề thi chất lượng từ các website bán tài liệu lớn.
Tải đề thi VIP với giá siêu rẻ tại Vip.Dethihsg247.Com