Extending YiiBase

A way to add static functions to YiiBase.




  Yii::extend(array('funcName' => function() {}));

  Yii::extend(array('className'));

  

  class className {

    static function foo() {}

  }


  

  Yii::funcName();

  Yii::foo();






class YiiBase {

...


  private static $_extendsMethods = array();

  private static $_extendsClasses = array();

  public static function extend($extends)

  {

      foreach ($extends as $name => $extend)

      {

          if (is_int($name))

              self::$_extendsClasses[] = $extend;

          else

              self::$_extendsMethods[$name] = $extend;

      }

  }

  public static function __callStatic($name, $arguments)

  {

      if (isset(self::$_extendsMethods[$name]))

      {

          call_user_func_array(self::$_extendsMethods[$name], $arguments);

      }

      else

      {

          foreach (self::$_extendsClasses as $class)

          {

              if (method_exists($class, $name))

              {

                  call_user_func_array($class . '::' . $name, $arguments);

              }

          }

      }

  }


...

}



It probably won’t happen often but you sometimes might want to extend YiiBase.

I need to extend Yii:t() but don’t really want to have it in a helper class.

Go to the source code, you have there Yii.php, it is blank class…

best place to hack the core :lol:

Trying to leave the framework folder alone.

Hacking the core only causes issue with new versions of Yii.

You can create your own Yii class and place it outside the framework folder. This is the way it was designed to work.

From documentation:

The only place you need to change in this case is your index.php.

I was just going to create my own Yii class to extend YiiBase, but doing so means I can’t use YiiLite.

You can generate your own yiilite. Check build directory in svn.

http://code.google.com/p/yii/source/browse/trunk/build/commands/LiteCommand.php

Didn’t know about that, not used Yii’s command line tools.