Changes
Title
changed
Singleton in YiiHow to write a simple application component
Category
unchanged
How-tos
Yii version
unchanged
Tags
changed
pattern singletonapplication component
Content
changed
SingletonApplication component
------------------
Singleton is a desing pattern that is very useful when exactly one object is needed to coordinate actiAn application component is a confortable way for share information amon
sg a
cross the system.
In Yii there are many implementation of this pattern, for example Yii::app()->db.
In this article we will learn how to create a singleton.ll component of the application
In Yii there are many application components, for example Yii::app()->db.
### Creating the class[...]
Let's imagine we need almost everywhere an instance of the class Region, and we want to avoid to do too much queries.
We can create a class which extends CApplicationCompomnent, like that:[...]
### Conclusion
This approach can be very useful and resource-saving, but is better to avoid to as it introduces global state into an application, and that can lead to bad practicesecause we do at most only a single query.