Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7.5k
Java Like Import
Derek Jones edited this page Jul 5, 2012
·
5 revisions
This is a primitive java like import for CI.
**CAUTION **: It may produce unpredictable result If you have two class with the same name (even if you put it in a different file/package).
You must enable hooks first at yout config.php
$config['enable_hooks'] = TRUE;Add a hooks configuration at hooks.php
$hook['pre_system'] = array(
'class' => '',
'function' => 'fake',
'filename' => 'ClassLoader.php',
'filepath' => 'hooks'
);Create ClassLoader.php under hooks directory
<?php
functionfake(){
// do nothing
}
functionimport($class, $package = 'package')
{ $class = $package . "/" . $class;
$class = str_replace(".", "/", $class);
$path = APPPATH . $class . EXT; if(file_exists($path)){
require_once($path);
returntrue;
}else{
returnfalse;
}
}
?>Create a folder named 'package' under application directory. This directory is the home for your own class.
Example: We create a class BaseController on package/com/company/project/controller/BaseController.php
<?php
// extends CI Controllerclass BaseController extends Controller {
functionBaseController(){
parent::Controller();
}
}
?>At your regular controller you can import base controller like this:
<?php
import('com.company.project.controller.BaseController');
// you can import other class hereclass Homepage extends BaseController{
functionHomepage(){
parent::BaseController(); }
functionindex(){
echo"test";
}
}
?>