Extending Core Classes

Be sure to know what you are doing, extended core methods under the same name will be used by the core as well as by your application and this may result in unexpected behavior.

Extending but not replacing the core class

These are the easiest and will work like any other class you create; just make them extend the core class in the global namespace:

class MyConfig extends Config {}

Extending & replacing core classes

If you want your core extension to be used by the core as well as by your own application you need to extend it under the same name but take it from the "Fuel\Core" namespace. Below is an example for the Lang class which you create in "fuel/app/classes/lang.php":

class Lang extends Fuel\Core\Lang {}

But classes that have the same name as core classes are ignored by default. To make the Autoloader recognize your replacement you need to register it in the application bootstrap file. Search for the lines below:

Autoloader::add_classes(array(
	// Add classes you want to override here
	// Example: 'View' => APPPATH.'classes/view.php',
));

As is explained in the comments you need to add the new Lang class as follows:

Autoloader::add_classes(array(
	// Add classes you want to override here
	// Example: 'View' => APPPATH.'classes/view.php',
	'Lang' => APPPATH.'classes/lang.php',
));

After this the core class will have been replaced by your extension.

The core class will still be available when used with full namespace prefixed. With the above example of extending "Lang" you can still use the original by calling "Fuel\Core\Lang".

Extending the Core from packages

By adding your package as a core namespace the autoloader will attempt to load any class from your package before it attempts it from the core. You must register these classes with the Autoloader though in order for them to be detected (filesystem autoloader doesn't support aliasing to global). Below is an example for extending the View class.

Autoloader::add_core_namespace('Example');

Autoloader::add_classes(array(
	'Example\\View'  => __DIR__.'/classes/view.php',
));

Extension limitations

All classes can be extended from app. Most classes can be extended from packages but there are a few exceptions:

  • Fuel
  • Config
  • Config_Php
  • Config_File
  • Config_Interface
  • Finder
  • Arr
  • Input
  • Security
  • Event
  • Event_Instance
  • And any class you use in your main app/config/config.php

If you have activated the profiler in your configuration, you can not extend these classes from packages too:

  • Cookie
  • Session
  • Session_Cookie (or another session driver class, depending on your session configuration)
  • Session_Driver
  • Date
  • Profiler

You can work around some of these limitations by manually load your package in the app/bootstrap.php file, by adding Package::load('mypackagename'); just before the Fuel::init() call. If you do, your package can only not extend:

  • Fuel
  • Config
  • Package
  • Arr
  • Finder

Autoloader

The Autoloader class is a special case, you can only extend it once as Autoloader and have it used. After extending it you have to require it manually in the app/bootstrap.php file after the original Fuel\Core\Autoloader, don't forget to remove the line that aliases the core class to global.