Yii 1.1: [Updated] How to create & call custom global function in whole application?


In this wiki I will show how to create own custom global function. It may be save the space and reduce the time.

In your components -

You need to make a file like - MyClass.php and put it inside components folder. Inside the MyClass.php you can write your functions. Like -

class MyClass extends CApplicationComponent {
 public function get_my_info() {
     //your code here
     //your code here
     return value;

In your main.php (config folder) -

'components' => array(
        'myClass' => array(
            'class' => 'ext.components.MyClass',

Now this function is accessible in whole application (controller, view etc.), Directly call the function get_my_info() any where and it will return your value. Like -

$myInfo = Yii::app()->myClass->get_my_info();

thanks to @hippyjim for better help... Try this and reduce the time :)...

Total 8 comments

#17636 report it
le_top at 2014/07/09 03:55pm
"Extending" Core Classes

@hippyjim: I agree that adapting core classes to application requirements is not always an easy job, even if in most cases Yii is pretty flexible. In the case of self-managed utility classes the comment of its limited flexibility in terms of adapting that class is limited because you have control over your source.

CApplicationComponent is something I use quite often - I even built a CApplicationComponentProxy to select the right component depending on the user configuration.

A utility class should be for "immutable" functionnality - I have functions in it for converting database time to system time for instance. Formatting is done in a Formatter component that replaces the standard one.

While it is a hack, it is still possible to replace "CHtml" for all classes if you explicitally load a replacement CHtml class before the autoloader looks for CHtml. A simple include of the file in your 'index.php' should do the trick. If you are using 'Yiilite', you need to rebuild Yiilite excluding the class that you want to replace.

If you just want to make some minor changes to CHtml while keeping the same class name, you can have a look at Patching php code without changing the original code. If that patch method can be avoided it is preferable, but avoiding a change to the original code might be preferable to allow easy vendor updates.

Components, utility classes and patches all have their use, in that preferred order ;-).

#17628 report it
Rohit Suthar at 2014/07/09 03:55am
help & suggestions

thanks for the valuable suggestions and help..

#17626 report it
egulhan at 2014/07/08 11:40pm
More convenient way

You should prefer functions of a class to global functions for these types of functions. I mostly keep my helper classes in ../app/protected/helpers/ like below:

class Common
 * Shortcut function to Yii::app()->user->checkAccess()
 * ...
public static function ca(...)
#17620 report it
hippyjim at 2014/07/08 10:42am
Object orientation and the Single(ton) guy

@le_top - you make a fair point about the overhead from using a component, I don't really have figures to see how much difference that would make so I can't comment further.

You quoted CHtml as a good example of using static/singleton (which it is), but singletons have their pitfalls. Singletons really just create globals, but with a prettier way to access them.

You mention creating a derived class of CHtml - that's good, but as CHtml is called explicitly, anything that uses the CHtml methods you want to override would have to be overridden as well (e.g. CGridview)!

In the past I've had to rebuild entire widgets because they call CHtml directly instead of using a configurable component, so I was just offering an alternative - and using CHtml as an example of something that's impossible to really replace in Yii without replacing everything that uses it.

Either way, I agree that your solution is definitely valid and an improvement on dumping a load of functions straight into the top level, I was just presenting another case/option.

#17619 report it
le_top at 2014/07/08 10:11am
CApplicationComponent vs. Utility Class

Using a CApplicationComponent may not be appropriate if the utility function is used "often" (because Yii::app()->myutils results in a function call to find the component, whereas the use of a utility class does not).

CHtml was mentionned as an example of a Utility class, not something to hack !

To extend/override CHtml, create a derived class and use that instead ;-).

#17612 report it
Rohit Suthar at 2014/07/08 03:58am
thanks for comments

Hey @hippyjim and @le_top, thanks for your valuable comments..

#17607 report it
hippyjim at 2014/07/07 05:12pm
Better still, use a component

Rather than creating more global scope or polluting the global namespace, use a component.

Using singletons or statics causes problems if you want to extend or override anything - try overriding CHtml without hacking at the autoloader!

If you use a component you can easily swap it out programmatically or in config.


class MyUtils extends CApplicationComponent {
 public function get_my_info() {
     //your code here
     //your code here
     return value;

Then set it up in config/main.php:

'components' => array(
        'myUtils' => array(
            'class' => 'ext.components.MyUtils',

And to use it:

$myInfo = Yii::app()->myUtils->get_my_info();
#17604 report it
le_top at 2014/07/07 03:24pm
Use a utility class in an object oriented environment

In an object oriented environment, it is best to use a utility class, much like the classes in 'yii/framework/utils' or like the 'CHtml' class for instance.

Using globals is to be avoided. Singletons and Utility classes are substitutes for globals and help avoid name clashes - you could even put your utility class in a namespace of your own.

Another advantage of using a Utility class is that you can rely on the standard class loading mechanism. Yii will take care of loading your class when you need it, not on every call.

So simply create a class like this in a file called 'MyUtils.php' for this example:

class MyUtils {
   public static function get_my_info() { /* rest of code */ return value; }

And use it like this:

   echo MyUtils::get_my_info();

Leave a comment

Please to leave your comment.

Write new article