Difference between #2 and #3 of
How to write a simple application component

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 amonsg across 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 CApplicationCompo
mnent, like that:
[...]
### Conclusion

This approach can be very useful and resource-saving, b
ut 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.
15 7
14 followers
Viewed: 75 727 times
Version: 1.1
Category: How-tos
Written by: zaccaria
Last updated by: zaccaria
Created on: May 6, 2011
Last updated: 13 years ago
Update Article

Revisions

View all history