[eluser]danmontgomery[/eluser]
He's specifically asking how not to use CI's autoload, and __autoload won't work because $this->db won't be defined if the database isn't loaded.
In 2.0 you can set the 'autoinit' configuration in config/database.php which will keep the database from connecting when the class is instantiated... This would probably be the most elegant way. I haven't found any way in 1.7.2 to properly set this (the check is made as soon as the class is loaded, line 135 of DB.php), but you could theoretically extend the DB driver and add the autoinit value, or just take some code from 2.0 (or upgrade/wait for 2.0).
I can't really think of a good way to load the class on demand... You would have to explicitly declare $this->db, and do something like:
Code:
if(!is_a($this->db, 'CI_DB_mysql_driver')) {
unset($this->db);
$this->load->database();
}
Which is probably pretty oversimplified.