Author: Carlos Artur Curvelo da Matos
Viewers: 755
Last month viewers: 7
Package: TonTon PHP Singleton Trait New Generation
These design patterns are very useful for PHP developers in certain circumstances.
Read this article to learn why the singleton and multiton can be useful in PHP projects and how you can use them in general purpose project and in particular in WordPress projects.
In this article you will learn:
What is this Article About
Singletons as a Simple Design Pattern
What Are Multitons
What if I want to Use a Specific Number of Class Objects?
Removing Redundant Code Using PHP Traits
How to Download the TonTon PHP Singleton Trait Package or Install it With PHP Composer
What is this Article About
If you have been following our posts, you probably noticed that this content is definitely not for those looking for the "best plugins" for WordPress or trying to find ways to troubleshoot problems that are often caused by those same plugins.
At the same time, we are not targetting experienced and top notch PHP developers or programmers, although they can always gain good knowledge, like us, from reading interesting new approaches and ideas on coding.
But no, if you are willing to really understand WordPress and other frameworks, as well as PHP, in a way that you can not find the solutions, but think about them, so you are definitely the one we have been writing for.
Singletons as a Simple Design Pattern
If you are relatively familiar with PHP and even WordPress coding, you already heard about the term Singleton. Maybe you heard something on Multiton, or even design patterns in PHP. If not, let's quickly recap.
Before explaining the Singleton briefly, let's give an overview about the design patterns. The Singleton is one of them.
A Singleton is a design pattern for creating objects which assures that only one object of its kind exists simultaneously. A lot of developers consider the Singleton pattern an anti-pattern, but sometimes it is pretty handy, even though it may affect the code modularity.
Singletons in PHP usually make use of a static property that holds a single instance of an object. This is an example of a very simple Singleton class:
In the WordPress context, especially while developing a plugin, sometimes singletons can be useful for logging features, assuring that events are triggered and expected behaviors occur on plugin's installation or removal, or even in the plugin's main class or init class.
However, many plugins seem to use it singletons too much. Sometimes we want to limit instances, but not necessarily to a single one. And other times, we want to create class objects just a few times and also want make sure each one of the instances is distinct. For such cases there is a have a related design pattern called Multiton.
What Are Multitons
Well, if we wanted to limit the number class objects that can be created, you may ask why designing a pattern that allow creating multiple class objects?
The Multiton can limit the class objects that are created not just in quantity. It can also associate a named key to each unique class object that is created.
Our implementation of a Multiton is similar to a Singleton, but with two main differences:
- The static property $instances is an array.
- The
instance()
orgetInstance()
method receives an argument.
Regarding the last Multiton aspect, the method that gets the instance would look like this:
What is the intention of this approach? Some classes may be used in different instances, for similar tasks that are not exactly same, like for instance multiple loggers objects or different PDO objects to access two or more existing databases, such as in the case of hybrid applications using MySQL and SQLite.
In WordPress, that could be handy for accessing two different MySQL databases like in sites using HyperDB, for example. Multiple logger objects can also be useful in many situations using a Multiton.
What if I want to Use a Specific Number of Class Objects?
Consider cases that you want to use not just a single class object, nor multiple class objects, but rather a specific number of instances defined by your application.
Multitons could be used, but you would need to have a way to limit instances when they reached the maximum number that you allow to exist.
That made us created a useful derivation of the Singleton pattern that we called the Limiton. In this case, on top of a key for each instance, we want to set a variable, which provides the maximum number of instances. Then check the count of instances stored in the $instances property before creating any new object. Something like this:
This simple and straightforward approach solves the issue. A Limiton could be useful in a number of situations. It limits the number of class objects that can be created, like the Singleton, but keeps a flag on each allowed instance that is created.
Besides that, a method setLimit()
provides additional flexibility by allowing to modify the number of concurrent object that can exist at the same time.
Removing Redundant Code Using PHP Traits
Nothing worse than finding an application or plugin in which at least a dozen of classes start with a Singleton inside them. That is a tragic and unnecessary copy and paste effort.
If you are familiar with PHP traits, that has been a smart way of avoiding code duplication. So let's use traits inside our classes to apply Singletons, rather than pasting it on every situation required.
Of course we provide a library for that: you can just use traits. a Singleton, a Multiton and our Limiton.
How to Download the TonTon PHP Singleton Trait Package or Install it With PHP Composer
The TonTon PHP Singleton Trait package is available for you to download as a ZIP archive by going to the download page or install it using the PHP Composer Tool by going to the installation instructions page.
You need to be a registered user or login to post a comment
Login Immediately with your account on:
Comments:
1. singleton initializer - adriano ghezzi (2021-04-29 11:47)
initialize singleton... - 0 replies
Read the whole comment and replies