Google News
logo
FuelPHP - Interview Questions
Explain Extending Core Classes in FuelPHP.
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
* Extending & replacing core classes
* Extending the Core from packages
* Extension limitations

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 {}
 
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
Advertisement