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

Revision #3 has been created by zaccaria on Jun 2, 2011, 6:31:38 AM with the memo:

it is not singleton
« previous (#2)

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: 74 720 times
Version: 1.1
Category: How-tos
Written by: zaccaria
Last updated by: zaccaria
Created on: May 6, 2011
Last updated: 12 years ago
Update Article

Revisions

View all history