This is documentation for Kohana v2.3.x.

Table of Contents
StatusDraft
TodoExpand examples, Proof read

Helpers

Helpers are simply “handy” functions that help you with development.

Helpers are similar to library methods, but there is a subtle difference. With a library, you have to create an instance of the library's class to use its methods. Helpers are declared as static methods of a class, so there is no need to instantiate the class. You can think of them as “global functions”.

As with libraries, the helper classes are automatically loaded by the framework when they are used, so there is no need to load them yourself.

Here is an example call to a helper:

// show the location of this Kohana installation
echo url::base();

As with most of Kohana, you can add your own helpers and replace or extend Kohana's built-in ones.

Adding your own helpers

When creating your own helpers, these are the conventions that are suggested:

For example, suppose that you wanted to create a helper to help with JavaScript, you might create the following file:

File: application/helpers/javascript.php

<?php defined('SYSPATH') or die('No direct script access.');
 
class javascript_Core {
 
	public static function alert($message)
	{
		return "alert('$message');\n";
	}
}
 
?>

and then to use your helper, you would do the following:

javascript::alert("Oh no!");

Extending helpers

Kohana also allows you to extend its built-in helpers so that you can add your own functionality to them. You should never change the files in system/helpers! Instead, you can create a new helper that extends a built-in helper.

You can also extend your own helpers, so long as you have added ”_Core” to the end of their class names.

When extending a helper, the conventions are the same as for when you are creating a new helper, with a couple of exceptions:

For example, lets suppose that you want to extend Kohana's HTML helper. You might do the following:

File: application/helpers/MY_html.php

<?php defined('SYSPATH') or die('No direct script access.');
 
class html extends html_Core {
 
	public static function your_custom_method()
	{
	}
 
}
?>

Extending the core classes is not only allowed in Kohana, but is expected.

Replacing Kohana's built-in helpers

It is also possible (although probably less often required) to replacing one of Kohana's built-in helpers entirely. The conventions are the same as when you are adding your own helper, with one exception:

If, for example, you want to replace the url helper, you might do something like:

File: application/helpers/url.php

<?php defined('SYSPATH') or die('No direct script access.');
 
class url_Core {
 
	// define your own url helper here
 
}
 
?>