This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
lnkohana/system/guide/kohana/files/classes.md
2013-04-22 14:09:50 +10:00

1.7 KiB

Classes

TODO: Brief intro to classes.

Models and Controllers are classes as well, but are treated slightly differently by Kohana. Read their respective pages to learn more.

Helper or Library?

Kohana 3 does not differentiate between "helper" classes and "library" classes like in previous versions. They are all placed in the classes/ folder and follow the same conventions. The distinction is that in general, a "helper" class is used statically, (for examples see the helpers included in Kohana), and library classes are typically instantiated and used as objects (like the Database query builders). The distinction is not black and white, and is irrelevant anyways, since they are treated the same by Kohana.

Creating a class

To create a new class, simply place a file in the classes/ directory at any point in the Cascading Filesystem, that follows the Class naming conventions. For example, lets create a Foobar class.

// classes/Foobar.php

class Foobar {
	static function magic() {
		// Does something
	}
}

We can now call Foobar::magic() any where and Kohana will autoload the file for us.

We can also put classes in subdirectories.

// classes/Professor/Baxter.php

class Professor_Baxter {
	static function teach() {
		// Does something
	}
}

We could now call Professor_Baxter::teach() any where we want.

For examples of how to create and use classes, simply look at the 'classes' folder in system or any module.

Namespacing your classes

TODO: Discuss namespacing to provide transparent extension functionality in your own classes/modules.