Including functions library

I just started testing the Yii framework to see if it meets the requirements of my project and so far so good. However, i have an enquiry. Is there a standard for including a function library in the "protected" directory?

My question might not be clear so let me try to clarify. I would like to have a file for example "functions.php" in which i have functions that i can call in any controller, model, component or view. Is there already a Yii standard for doing this? (i want to accomplish this without adding anything to the yii core files).


Since these functions are global, you can create a new directory under protected directory (e.g. ‘global’) and include all necessary files in the index.php.

I’d strongly recommend that since we are in oo-land with Yii, any methods should reside in their appropriate class.

However, you might have some language constructs, and I see no reason not to put them in you application’s index.php. For example,

function app() { return Yii::app(); }

function user() { return Yii::app()->user; }

I’m sure you know, but breaking encapsulation reduces code portability and makes testing more difficult.

There’s never a need to change the core, Yii is oo, so just extend the appropriate core class and use that.

If you are unfamiliar with oo, then the least worst thing you could do, is create Utils class and call your "functions" statically on that (i.e. Utils::someFunction($some_params)). I often start a project with such a class, but have the aim of removing it before going live. Util functions always have somewhere to live once you understand their behaviour better.