[eluser]Jelmer[/eluser]
[quote author="Phil Sturgeon" date="1264617214"]Tuesday[/quote]
LOL
[quote author="GSV Sleeper Service" date="1264617034"]@Jelmer, thanks, I never considered trying that. Won't you end up with two instances of the object if you do that though?
I have set up my own __autoload() to replace the CI models with Doctrine models, this works fine.[/quote]
If you use the $this->load->library('class') you'll indeed end up with 2 instances, which is why I use an autoloader. But that first example is for when you need a second instance, otherwise I don't really see a problem using the normal CI syntax (other than maybe your IDE).
I'm currently using RapidDataMapper for my projects and I've changed the default autoloader in the MY_Loader class to the following to allow for loading non-core libraries & models. You could probably change your Doctrine autoloader like this to allow for autoloading libraries and models.
Code:
public static function load_data_object($class)
{
$class = preg_replace('/([^a-zA-Z0-9_]*)/', '', $class);
$class_db = strtolower($class);
if (file_exists(APPPATH . 'data_model/'.$class_db.EXT))
{
require APPPATH . 'data_model/'.$class_db.EXT;
return TRUE;
}
elseif (file_exists(APPPATH.'libraries/'.$class.'.php'))
{
require_once(APPPATH.'libraries/'.$class.'.php');
return TRUE;
}
elseif (file_exists(APPPATH.'models/'.strtolower($class).'.php'))
{
$ci = get_instance();
$ci->load->model($class);
unset($ci->{$class});
return TRUE;
}
}
(I put models at the bottom as that's the least common usage and data_models are used more often than libraries)
I also added an autoloaded helper that allows me to call CI functions in the same way from everywhere using a static class:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ci
{
// Protect constructor to prevent instantiation
private function __construct() { }
// Get CI library/model or variable
static function get($name)
{
$ci = get_instance();
return $ci->{$name};
}
// Alias for get(), only allows objects
static function obj($name)
{
$return = self::get($name);
if (is_object($return))
return $return;
else
return NULL;
}
// Alias for obj()
static function lib($name)
{
return self::obj($name);
}
// Alias for get(), only allows extentions of the Model class
static function mod($name)
{
$return = self::get($name);
if (is_subclass_of($return, 'Model'))
return $return;
else
return NULL;
}
// Load CI application library (not used for system libraries)
static function library($library = '', $params = NULL, $object_name = NULL)
{
$ci = get_instance();
return $ci->load->library($library, $params, $object_name);
}
// Load CI application model
static function model($model, $name = '', $db_conn = FALSE)
{
$ci = get_instance();
return $ci->load->model($model, $name, $db_conn);
}
// Load CI application view
static function view($view, $vars = array(), $return = FALSE)
{
$ci = get_instance();
return $ci->load->view($view, $vars, $return);
}
// Load CI application helper
static function helper($helpers = array())
{
$ci = get_instance();
return $ci->load->helper($helpers);
}
// Get config item
static function config($item, $namespace = NULL)
{
$ci = get_instance();
return $ci->config->item($item, $namespace);
}
// Get language line
static function lang($line)
{
$ci = get_instance();
return $ci->lang->line($line);
}
// Get post variable
static function post($var)
{
$ci = get_instance();
return $ci->input->post($var);
}
}
/* End of file short_ci_helper.php */
/* Location: ./system/application/helpers/short_ci_helper.php */
It makes some statements actually longer but it looks better in my opinion, especially in libraries which I used to give a property $this->ci and as such every CI function became something like
$this->ci->config->item('') which became
Ci::config('') or libraries where
$this->ci->image_lib->clear() became
Ci::lib('image_lib')->clear(). In controllers though
$this->image_lib->clear() is actually shorter than
Ci::lib('image_lib')->clear().
I didn't do it because it saves characters but because it makes accessing CI look the same everywhere and because it more clearly differentiates controller functions from CI functions.
Loading libraries/models/helpers is shorter though (Ci::library(), Ci::model(), Ci::helper) as is accessing language lines, post variables and config items (Ci::config(), Ci::lang(), Ci::post())