Global Function And Data

where can I put global static functions and data to use for many models and controllers?

Is there file (class or not) seperated for controllers and models to achieve that or I should make my own one?

For controllers, you can use protected/components/controller.php that extends CController.

For models, you can extend CActiveRecord (or GxActiveRecord if you’re using Giix) and make your models extend the new class.

For Controllers ok, I got it!

so, for models I have to create my own class model like myCActiveRecord extends the CActiveRecord (or CFormModel) and use it for all my models, right?



If you want to have genuinely global static functions, you can also create one or many helper classes depending on how much structure you require. This will enable you to keep closely related functions together. Such classes could be put into your components folder, or under a separate "helpers" folder in your app directory.

Yes it works.

The only problem with class in Component folder is the initializing of class from Yii

(I should make blank init() function too)

The other confusing thing is calling of function

Yii::App()->MyGlobalclass::myfunction() (it not works) but Yii::App()->MyGlobalclass->myfunction() (it work) confusing me because myfunction() seems like not static function (may issue of php version)

In other respects it’t a nice approach, thanks!

If you have any comment of above issues, please inform me!

No, you’d extend from CComponent, then call the class directly:

class MyHelperClass extends CComponent


    public static function myMethod()


        return 'cheese';



$result = MyHelperClass::myMethod();

That’s assuming that the class appears in your components folder, which is automatically imported by Yii.

You need to extend CComponent for Yii’s magic methods to work.

The file should be called MyHelperClass.php

By the way, there’s also a wiki on shortcut functions and YiiBoilerplate has nice ones:

both bennouna’s and Keith’s approaches are useful