appliation global variables

is therre a way i can make application wide globals ie accessable via Yii::app()->variable?

basically i have a $state variable i need to access anywhere in the application thtas tied to the uers so maybe Yii:app()->user->state. but the state comes from an unrelated model, do i have to load in this model everytime i make a request, or can i stick the static variables in a static class.



Or you can use the user session with setState()

Is it possible to store an object in set state, or do only variables work?

setState() stores values in session ($_SESSION)…

it is possible to store even objects but it’s not a good practice… it’s better to store only some ID for example (PK)… and then recreate the object…

You can try, if it works is ok. If you have problem, you can use serialize and unserialize for repair.

set variable:

Yii::app()->params[‘abc’] = 123;

get variable:

echo Yii::app()->params[‘abc’];

Although my taste is to have them in the main.php config file for each application.

Yii::app()->params[‘abc’]= 123

echo Yii::app()->params[‘abc’];

This is Handy !


If you want to add number of variable you can extend the class CApplicationComponent



and do your stuffs as your wish.

below an example

class vars extends CApplicationComponent {

	public $your_variable = 'Your Value';

	// and so ON...

	//Also your functions


So you can access the variable $var1 as follows

 echo Yii::app->vars->your_variable;

It gives the output : Your Value


[/b]I tested its working fine even you can set and get values for that variable.

If you like Don’t forget to press +1



Cheers !