What is a class?

A class is just a normal PHP class. It doesn't need to extend anything or follow any conventions other than the naming convention which is the same as all other classes in Fuel.

class Session

That will be loaded from app/classes/session.php.

Loading Classes

Unlike some other frameworks classes do not need to be loaded manually. They will be auto-loaded when you refer to them in your code (Controllers, Models, etc).

Classes in a sub-directory

Just like Controllers, classes must be lower-case with first-letter upper case, while underscores will put the class into a sub-directory.

Class Session_Driver

That will be loaded from app/classes/session/driver.php

Classes and Namespaces

To determine which PHP file to load, FuelPHP's autoloader will treat namespaces and class names with underscores exactly the same. This means that for classes in sub-directories, you can mix-and-match namespaces and underscores to suit your needs.

Take for example a class in the file app/classes/core/system/messages.php. The class in this file can be defined as:

// global namespace, fully underscored class name
class Core_System_Messages {}

// combine a namespace and underscores
namespace Core;
class System_Messages {}

// or fully namespaced
namespace Core\System;
class Messages {}

The first method is the one most commonly used and easiest to understand. The namespaced versions are particularly handy if you combine them with the Use statement in the classes that use the defined class.

Initializing your class

It is possible to have Fuel's autoloader perform certain automated tasks once a class is loaded, sort of like what __construct() does for instances of a class. You do this by adding a public static _init() method to the class.

class Example {
	public static function _init()
		// this is called upon loading the class

If a loaded class has its own _init() method and a parent must be loaded alongside it, that parent's init method isn't called unless you call it using parent::_init();